ios - Sprite Kit - Do not allow node to move out of screen -
i creating game not want player able move out of screen. node follows players moving touch. code have "under construction" player can't move out on top or right side, not want player able move out on of sides.
- (void)movementplayer { skaction * actionmovex = [skaction movetox:min(location2.x - playerpositionx, self.size.width - (_player.size.width/2)) duration:0]; skaction * actionmovey = [skaction movetoy:min(location2.y - playerpositiony, self.size.height - (_player.size.height/2)) duration:0]; [_player runaction:[skaction sequence:@[actionmovex, actionmovey]]]; }
you should create physics world , add border rectangle around screen. border must have physics body set collide physics body collision category given player node. if player node starts inside border, player cannot leave screen , no additional coding required (besides setting collision categories each physics body)
raywenderlich.com has easy understand game tutorials show how handle collisions following sprite kit manual.
Comments
Post a Comment