0

I'm trying to have a button that when pressed, will reset the scene. The button works fine however on the second time, certain nodes are in different positions and physics contacts are being triggered. Here's my code

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */



    if (inGame == false) {

        inGame = true;
        gravity = -7;
        SKAction *wait = [SKAction waitForDuration:1.5];
        SKAction *moveOff = [SKAction moveBy:CGVectorMake(-25000, 0) duration:100];
        SKAction *sequence = [SKAction sequence:@[wait, moveOff]];
        [startRamp runAction:sequence];
        [tapToStart runAction:sequence];

        SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"Ball"];
        ball.position = CGPointMake(CGRectGetMaxX(self.frame)/(CGRectGetMaxX(self.frame)/64), CGRectGetMidY(self.frame)*1.3);
        ball.scale = CGRectGetMaxX(self.frame)/((CGRectGetMaxX(self.frame)*8));
        ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2];
        ball.physicsBody.categoryBitMask = playerMask;
        ball.physicsBody.contactTestBitMask = playerMask2;
        [self addChild:ball];

        nodeLocation = ball;
        ball = nodeLocation;

    }

    else if (timeCountStart >= 90) gravity *= -1;


    else if (moveNextScene == true) {



        [self.view presentScene:self.scene transition:[SKTransition doorwayWithDuration:2]];

                }
moveNextScene = true;

       }
  • Take at look at this one http://stackoverflow.com/questions/29449164/skshapenode-circle-not-appearing-circular/29450203#29450203 likely want to use self.size and set the same aspect ration – Skyler Lauren Apr 06 '15 at 04:02
  • @SkylerLauren, the positioning is right now, but the gravity is a lot higher and the ball(My Node) is spazing out. – TheCommonGiraffe Apr 06 '15 at 14:02
  • You might have to see if you are doing anything else in your view controller to your scene. Not likely, but you might be setting stuff in there that will need to be done like you are doing with aspect ratio. – Skyler Lauren Apr 06 '15 at 14:04
  • @SkylerLauren So I've done some troubleshooting and found out that my actions from the previous scene are running in the next scene, any idea how to fix that? – TheCommonGiraffe Apr 07 '15 at 01:01
  • That is a neat issue. You could try self.pause and that will pause all actions and child actions. I believe that will allow the scene to get released properly when the scene loses reference to it. If not you would call `[NodeDoingActions removeAllActions]` However, your actions in one scene should not be causing issues in the next scene. – Skyler Lauren Apr 07 '15 at 01:08
  • @SkylerLauren, I've narrowed the problem down to something with TouchesBegan. I made it so that the ball is added on first touch, and scene is changed on second(For testing). It works fine the first time however after the scene change it completely ignores TouchesBegan and won't even add the ball again. – TheCommonGiraffe Apr 07 '15 at 02:19
  • You code has changed from your original question so I can't really see what it could be :/ – Skyler Lauren Apr 07 '15 at 02:22
  • @SkylerLauren, I added the touches began, brief overview, gravity is an int that controls gravity, the actions move everything away when screen is tapped, and time count start counts how many seconds since initial tap(not relevant here, just wanted to add everything though). – TheCommonGiraffe Apr 07 '15 at 02:26
  • Heh. Kind of a long shot but how are `inGame` and `gravity` declared? Take a look at this http://stackoverflow.com/questions/13566862/where-to-put-ivars-in-modern-objective-c is it code sample 1,2,or 3? – Skyler Lauren Apr 07 '15 at 02:33
  • It doesn't appear to be any of those. They are statically(If that's even proper spelling) declared at the top of the class – TheCommonGiraffe Apr 07 '15 at 02:40
  • I am willing to bet that is the issue then. Your are likely creating constants. For instance you are counting on inGame to be false when you start and it is the first time you instantiate that class. You set it to true when you press the button. Then when you load the next scene it is still true because it is a constant and not an instance variable (iVar) Things like that should not be static they should be properties (in my opinion) or at the least true instance variables. – Skyler Lauren Apr 07 '15 at 02:45
  • @SkylerLauren I finally got it to work! Turns out I never set inGame to false inside didMoveToView(Literally cried at my own stupidity for that). Anyway sorry for wasting your time and thank you. – TheCommonGiraffe Apr 07 '15 at 02:57
  • Glad to hear you got it working, but as I said before you do not want to keep them statically declared. That will mess you up in the future. It also explains why actions were still going and physics wasn't right. When you have static variables you always have to worry about what they were because they may already be set when you don't expect them to be. Either way I am glad I was able to help and good luck with your app =) – Skyler Lauren Apr 07 '15 at 03:01

0 Answers0