2
votes

What I have:

  • You click and a ball moves to that location.

  • The screen is split horizontally between upper & lower.

  • Let's say that the ball is on the lower side and you can't click on the lower side to have it move. You have to click the top side of the screen. This flips with regards to upper or lower.

What I'm going for:

  • The x coord doesn't change at all.

  • When the ball hits to top, it would change directions and go back down with out a click

  • Remove the UITouch location variable

  • Upper and lower stays system stays

    Every thing helps thank u very much.

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        /* Called when a touch begins */
        //SKAction *action = [SKAction rotateByAngle:M_PI duration:1];
        //[sprite runAction:[SKAction repeatActionForever:action]];
    
        for (UITouch *touch in touches) {
            location = [touch locationInNode:self];
        }
    
        float ballVelocity = self.frame.size.height/3.0;
        CGPoint moveDifference = CGPointMake(location.x - ball.position.x,location.y - ball.position.y);
        float distanceToMove = sqrtf(moveDifference.x * moveDifference.x +moveDifference.y * moveDifference.y);
        float moveDuration = distanceToMove / ballVelocity;
    
    
        Act_Move = [SKAction moveTo:location duration:moveDuration];
        Act_MoveDone = [SKAction runBlock:^(){
        NSLog(@"stoped");}];
        ActballMoveSeq = [SKAction sequence:@[Act_Move,Act_MoveDone]];
    
    
        if(((location.y>screenSize.height/2)&&(ball.position.y<screenSize.height/2))||((location.y<screenSize.height/2)&&(ball.position.y>screenSize.height/2))){
    
            if(canTap == true){
                [ball runAction:ActballMoveSeq withKey:@"moveBall_seq"];
            }
        }
    }
    
1
Not sure what you are asking help on. You want your ball to go up, hit the top and then come back down on just the x axis?sangony
the x axis i don't want to change just the y axischeesey
Ok but I am still unsure on what exactly you are asking. You need to better explain what it is you are looking to do. If you want the ball to go up and down why not just apply an impulse? Why are you specifically referencing blocks in your question?sangony
ill make a new question maybe change my code then ill find a way to link it to u sorry new to stack over flowcheesey
Thank u i didn't think to set gravity to 0 then the apply impulse but how do i grant u credit to the question ?cheesey

1 Answers

2
votes

To make the ball move up a certain distance and come back down on its own accord, use applyImpulse with your node.

// modify the dy value (100) to whatever value suits your needs
[myNode.physicsBody applyImpulse:CGVectorMake(0, 100)];

As long as your node is affected by gravity, it will eventually come back down.