Question

I realize sprite kit is the way to go for games, but i was wondering if there was way using UIDynamicAnimator to set up additional constant forces besides gravity. I am making a platform game and have set the gravity downwards. I have sections of the screen setup to represent arrow keys (press the top of the screen to jump, sides to move left/right...). So far i have used the UIPushBehavior to jump and pulse left and right.

What i would like to do is have the view (player) constantly move left or right if the key is down. I would imagine that this would require a second sideways "gravity". I would turn this on when left section of the screen is touched and off when the touch is released. Any ideas on alternatives to this? Apparently you cannot make 2 gravities. I was thinking if you cant do 2 forces, maybe a way to set a constant speed for the view in a direction.

edit: i guess another way to phrase this would be... Is there a way to just straight up set the velocity for a view?

Was it helpful?

Solution

You can use a UIPushBehavior:

UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[self.viewToAccelerate] mode:UIPushBehaviorModeContinuous];
pushBehavior.angle = M_PI_4;
pushBehavior.magnitude = 0.3;
[self.animator addBehavior:pushBehavior];

You just change the mode to UIPushBehaviorModeContinuous to make a constant force. Play with the magnitude and angle until it matches your needs. This will apply constant acceleration to your view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top