objective c - Add boundaries to an SKScene -


i'm trying write basic game using apple's sprite kit framework. far, have ship flying around screen, using skphysicsbody. want keep ship flying off screen, edited update method make ship's velocity zero. works of time, every , then, ship fly off screen.

here's update method.

// const int x_min = 60; // const int x_max = 853; // const int y_max = 660; // const int y_min = 60; // const float ship_speed = 50.0;  - (void)update:(cftimeinterval)currenttime {     if (self.keyspressed & down_arrow_pressed) {         if (self.ship.position.y > y_min) {             [self.ship.physicsbody applyforce:cgvectormake(0, -ship_speed)];         } else {             self.ship.physicsbody.velocity = cgvectormake(self.ship.physicsbody.velocity.dx, 0);         }     }      if (self.keyspressed & up_arrow_pressed) {         if (self.ship.position.y < y_max) {             [self.ship.physicsbody applyforce:cgvectormake(0, ship_speed)];         } else {             self.ship.physicsbody.velocity = cgvectormake(self.ship.physicsbody.velocity.dx, 0);         }     }      if (self.keyspressed & right_arrow_pressed) {         if (self.ship.position.x < x_max) {             [self.ship.physicsbody applyforce:cgvectormake(ship_speed, 0)];         } else {             self.ship.physicsbody.velocity = cgvectormake(0, self.ship.physicsbody.velocity.dy);         }     }      if (self.keyspressed & left_arrow_pressed) {         if (self.ship.position.x > x_min) {             [self.ship.physicsbody applyforce:cgvectormake(-ship_speed, 0)];         } else {             self.ship.physicsbody.velocity = cgvectormake(0, self.ship.physicsbody.velocity.dy);         }     } } 

at first, used applyimpulse in didbegincontact push ship back. made ship bounce, don't want ship bounce. want stop @ edge.

what right way make ship stop once reaches edge? code above works of time, every , ship shoots off screen. os x—not ios—in case matters.

check out link... ios7 skscene how make sprite bounce off edge of screen?

[self setphysicsbody:[skphysicsbody bodywithedgeloopfromrect:self.frame]];  //physics body of scene 

this should set barrier around edge of scene.

edit: example project apple might useful https://developer.apple.com/library/mac/samplecode/spritekit_physics_collisions/introduction/intro.html


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -