Move sprite up while tapping/holding in Cocos2D for iPhone - iphone

I was wondering how to have it in Cocos2D to make a sprite move up, while the user's finger is being held on the screen. I have a mechanism in place that does so, but only moves the sprite once.
Is it possible to have the sprite move up while the finger is holding on the screen?
Also, even though it's not necessary, if you could make it so when the user isn't holding on the screen, the sprite goes down.
Thanks, I think that a physics engine such as Box2D of Chipmunk is necessary, but I'm not sure how to do this.
-Soule

I have a not so perfect answer:
set up a repeatable timer with a short interval, at most 0.1 second, then fire the timer in ccTouchBegan, invalid the timer in ccTouchEnded. Everytime the timer ticks, move the sprite a short distance.
the shorter the interval is, the smoother the move will be.

Related

Draw trail behind player when its not moving

My game main player is sometime moving and some time in game play remain at same position. I have added trail renderer as child gameobject but when player is moving only that time trail get drawn as player stop its movement trail slowly become invisible.
This thing I don't want, I want always at least some trail get drawn with player. But how to do this?
To make the trail persist, increase the "How long does the trail take to fade out." docs - trailRenderer time
Now the trail stays longer/forever. It won't vanish when you move a lot, too.
Maybe you want distance-vanish instead of time vanish.
You can iterate over the points and compare distances. then dissolve them.
I think what you need is a particle system with the trail renderer module activate. For the effect you showed you could make it sending a single particle in the downward direction and using a trail to follow it.
Edit: Here is an image showing where the module is. You will need to tweak a little while to get your desired effect but it looks doable. Have fun!

Bullet firing in direction of rotation

I have a game where the main character is on the far left side of the screen (landscape) in the middle. He rotates using the accelerometer. Hes going to be shooting bullets constantly and I need a way to make the bullets fire depending on his rotation.
So basically I dont know the math to make the bullet fire straight depending on the sprites rotation.
Any help appreciated.
Edit:
I know my question is kind of vague, I dont really know how to ask it.
My character is a top view of a turret, he will rotate when you rotate your phone, and obviously as he rotates, the bullets still need to shoot straight. I just dont know the math to make this happen.
I assume you know how get values from accelerometer and the character "rotates" up and down (angry birds style).
If you fire your bullets by some amount of coordinates (moveBy(x,y) ) - just use more y to fire up. To fire down use more x.
For example: To fire in straight line - moveBy(10,10)
To fire up - moveBy(10,20);
To fire dowp - moveBy(20,10);
General case - moveBy(x*accelerometerVal, y*(1/accelerometerVal) );

Preventing jump more than once

I am writing an iphone game using cocos2d and box2d engines.
I have a ball which can be jumped by tapping the screen. The jump is applied by using ApplyImpulse method on the ball's body.
Thing is I do not want the ball to jump more than once. (i.e. If the ball is in mid-air I do not want it to be jumped again when tapping the screen.)
Is there a way to know when an object is in mid-air ?
Is there a common solution to this problem ?
Thanks
You can raycast from the center of the ball, downwards. If it collides with anything at a distance greater than the ball's radius, that means it is in the air.
To raycast, check chapter 10 in the manual here:
http://box2d.org/manual.pdf
It's in c++ but you should find similar examples in objective-c.
Hope it helps.

Dragging a Sprite (Cocos2D) while Chipmunk is simulating

I have a simple project built with Cocos2D and Chipmunk. So far it's just a Ball (body, shape & sprite) bouncing on the Ground (a static line segment at the bottom of the screen).
I implemented the ccTouchesBegan/Moved/Ended methods to drag the ball around.
I've tried both:
cpBodySlew(ballBody, touchPoint, 1.0/60.0f);
and
ballBody->p = cgPointMake(touchPoint.x,touchPoint.y);
and while the Ball does follow my dragging, it's still being affected by gravity and it tries to go down (which causes velocity problems and others).
Does anyone know of the preferred way to Drag an active Body while the physics simulation is going on?
Do I need somehow to stop the simulation and turn it back on afterwards?
Thanks!
Temporarily remove the body from the space.
If you want the object to have inertia when you release it, that's a different story. The cleanest way is to attach a fairly stiff spring between the ball and a temporary sensor body that moves under the control of your finger. When you let go with your finger, the ball will retain whatever kinematics it had while you were dragging it. Be sure not to remove the ball from the space in this case.
You aren't updating the velocity of the object when you aren't using cpBodySlew(). That is why it falls straight down.
A better way to do it is to use a force clamped pivot joint like the official demos do to implement mouse control. http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/ChipmunkDemo.c#296

Sprite Delay

i have a sprite & a set of coordinates... I want my sprite to follow that path & its following but its so fast that i can see only the last position......
So i want to introduce delay in every new position.. so how i can achieve this
Short ansfer: NSTimer. Update your sprite position on every timer tick. Generally, you want to have a master timer/clock that fires X times per second and update all your animation logic based on that clock. 30fps is good for games.
If you're doing a game, have you checked out cocos2d-iphone?