ios - Adding the NSObject to the a SKScene -


i making racing game using spritekit. create nsobject class called "gameobject" store properties, physics. create nsobject class called "gameworld" store game objects, creating player objects , other objects location , functions of direction buttons. however, when start write game scene class, cannot add world object scene, means car doesn't show in scene. can me question? codes of skscene class provided below,

@interface gameplay : skscene @property (nonatomic) gameworld *world; -(void)update:(nstimeinterval)currenttime; -(id) initwithsize:(cgsize)s andworld:(gameworld *)w; @end  @implementation gameplay  - (id) initwithsize:(cgsize)s andworld:(gameworld *)w {     self = [super initwithsize:s];     if (self)     {         _world = w;     }     return self; }  -(void) didmovetoview:(skview *)view {     if (!self.contentcreated ) {         [self createscenecontents];         self.contentcreated = yes;     } }  -(void) createscenecontents {     // turn off gravity world     self.physicsworld.gravity = cgvectormake(0.0f, 0.0f);     self.scalemode = skscenescalemodeaspectfit;      //create background     skspritenode *background = [skspritenode spritenodewithimagenamed:@"road.png"];     background.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame));     [self addchild:background];      //create buttons directions     skspritenode *up = [skspritenode spritenodewithimagenamed:@"up.png"];     up.position = cgpointmake(290, 115);     up.name = @"updirection";     [self addchild:up];      skspritenode *down = [skspritenode spritenodewithimagenamed:@"down.png"];     down.position = cgpointmake(290, 40);     down.name = @"downdirection";     [self addchild:down];      skspritenode *left = [skspritenode spritenodewithimagenamed:@"left.png"];     left.position = cgpointmake(30, 40);     left.name = @"leftdirection";     [self addchild:left];      skspritenode *right = [skspritenode spritenodewithimagenamed:@"right.png"];     right.position = cgpointmake(cgrectgetmidx(self.frame), 40);     right.name = @"rightdirection";     [self addchild:right];      // add objects gameworld scene using world's worldnode property     // here place confuse. world doesn't show      [self addchild:_world.worldnode]; }  //making buttons cars -(void)touchesbegan:(nsset *)touches withevent:(uievent *)event {     uitouch *touch = [touches anyobject];     cgpoint positioninscene = [touch locationinnode:self];      //setting node keep position of each direction buttons     sknode *n = [self nodeatpoint:positioninscene];      nslog(@"node n:%@", n);     //compare location of each skspritenode , touch location     sknode *up = [self childnodewithname:@"updirection"];     if (n == up) {         [self.world goforward];     }      sknode *down =[self childnodewithname:@"downdirection"];     if (n == down) {         [self.world gobackward];     }      sknode *left = [self childnodewithname:@"leftdirection"];     if (n == left) {         [self.world goleft];     }      sknode *right = [self childnodewithname:@"rightdirection"];     if (n == right) {         [self.world goright];     } }  -(void)update:(nstimeinterval)currenttime {     //i don't know how start yet. = = }  @end 

the gameworld class provided below:

@interface gameworld : nsobject @property (nonatomic) sknode *worldnode; @property (nonatomic) nsarray *gameobjs; -(id)init; -(void) initializeworld; -(void) goforward; -(void) gobackward; -(void) goleft; -(void) goright; @end  @implementation gameworld  -(id)init {     if (self) {         self = [super init];         gameobject *player = [[gameobject alloc] initwithimagenamed:@"pcar.png" andposition:cgpointmake(200, 85)];         gameobject *othercar = [[gameobject alloc]initwithimagenamed:@"aicar.png" andposition:cgpointmake(200, 100)];          _worldnode = [sknode node];         // create objects, put them in sknode         // create _worldnode size equal virtual world size         // add game objects node         _worldnode.scene.size = cgsizemake(320, 480);         _gameobjs = @[player, othercar];     }     return self; }  - (void) initializeworld {     // add skspritenodes worldnode     skspritenode *player = [[skspritenode alloc] initwithimagenamed:@"pcar.png"];     [_worldnode addchild:player];      skspritenode *othercar = [[skspritenode alloc] initwithimagenamed:@"aicar.png"];     [_worldnode addchild:othercar]; }  -(void) goforward {     gameobject *theplayer = _gameobjs[1];     theplayer.node.position = cgpointmake(theplayer.node.position.x, theplayer.node.position.y + 10); }  -(void) gobackward {     gameobject *theplayer = _gameobjs[1];     theplayer.node.position = cgpointmake(theplayer.node.position.x, theplayer.node.position.y - 10); }  -(void) goleft {     gameobject *theplayer = _gameobjs[1];     theplayer.node.position = cgpointmake(theplayer.node.position.x - 10, theplayer.node.position.y); }  -(void) goright {     gameobject *theplayer = _gameobjs[1];     theplayer.node.position = cgpointmake(theplayer.node.position.x + 10, theplayer.node.position.y); }  @end 

there no need use nsobject cases. use sknode , skspritenode instead. subclassing these classes can add custom properties them.

i suggest following structure of project:

-- gamescene (skscene) ---- backgroundlayer (sknode) -------- backgroundnode (skspritenode) ---- uilayer (sknode) -------- buttons here... (skspritenode) ---- carlayer (sknode) -------- playernode (skspritenode) -------- othercarnode (skspritenode) 

in case don't have store objects in manually created arrays, such gameobjs. example, buttons can accessed property: uilayer.children.

i have created template, use sprite kit games, take on it:

https://github.com/andrew8712/spritekit-game-template


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -