开发者

How to use a SneakyJoystick to shoot bullets (cocos2d for iphone)?

I want to use a sneakyjoystick to shoot bullets. Every time the joystick is active it should shoot a bullet from the player into the direction that the joystick points.

How can I do that?

PS: It would be great if someone could explain the code. I want to understand it ;)

Edit:

static CGPoint applyVelocity(CGPoint开发者_如何学编程 velocity, CGPoint position, float delta){
    return CGPointMake(position.x + velocity.x * delta, position.y + velocity.y * delta);
}
-(void)canShootWithRightJoystick { //called in tick method
    if (hudLayer.rightJoystick.velocity.x != 0 && hudLayer.rightJoystick.velocity.y != 0) {

        CCSprite* sp = [CCSprite spriteWithFile:@"red.png"];
        sp.position = player.position;
        [self addChild:sp z:10];

        CGPoint vel = hudLayer.rightJoystick.velocity;
        CCLOG(@"%.5f //// %.5f",vel.x,vel.y);
        vel = ccpMult(ccpNormalize(vel), 50);
        sp.position = applyVelocity(vel,player.position, 50);

    }
}

But nothing happens :(


Do you want to understand the code written above?? If yes then here is some description,

There are two functions in code above. The first function "applyVelocity" takes the "velocity", "current position" and "dela" and returns a new position after adding velocity and delta to current position.

Please note that the Sneaky Joystick gives you the velocity (direction and magnitude), that you have to use to see which direction to move and how much to move.

 if (hudLayer.rightJoystick.velocity.x != 0 && hudLayer.rightJoystick.velocity.y != 0)

The above line checks if the X and Y component of your velocity is NOT ZERO, which means that joystick is active and is moving.

CCSprite* sp = [CCSprite spriteWithFile:@"red.png"];
sp.position = player.position;
[self addChild:sp z:10];

Here you create a bullet sprite "sp" and in you set its position to same position as of player and then you added that sprite to layer so that its visible.

Now your bullet sprite is created and you have to move it so,

CGPoint vel = hudLayer.rightJoystick.velocity;
CCLOG(@"%.5f //// %.5f",vel.x,vel.y);
vel = ccpMult(ccpNormalize(vel), 50);
sp.position = applyVelocity(vel,player.position, 50);

In above code you get the velocity from your joystick, it gives you the current state of joystick and then multiply that velocity vector with 50 (your choice), and then you call your "applyVelocity" function to calculate the new position of your bullet sprite and you assign that new postion to your bullet "sp.postion".


Now I assume that you are calling "canShootWithRightJoystick" function in your tick() method as mentioned in your code comment. So this function will be called again and again as with interval that you specified in your schedule selector. Whenever it is called it checks the joystick velocity and creates a new bullet sprite and update its location.

But the problem is that you are creating a sprite at location A and then immediately changing its location to B. So you won't see your bullet traveling from A to B. And if your calculate point B is away from screen then you will see nothing at all.

Check your console logs and add new logs to see the values of "sp.position" before applying velocity and after applying velocity like,

CCLOG(@"Postion of bullet before: %.5f //// %.5f",sp.position.x,sp.position.y);
sp.position = applyVelocity(vel,player.position, 50);
CCLOG(@"Postion of bullet after: %.5f //// %.5f",sp.position.x,sp.position.y);

Replace your last line of "canShootWithRightJoystick" function with code above and see the output in console.

I hope it helps.


EDIT: The CCMoveBy solution worked for animating the bullet with sneakyInput Joystick. Below is the updated "canShootWithRightJoystick" function,


-(void)canShootWithRightJoystick { //called in tick method
    if (hudLayer.rightJoystick.velocity.x != 0 && hudLayer.rightJoystick.velocity.y != 0) {
        CCSprite* sp = [CCSprite spriteWithFile:@"red.png"]; 
        sp.position = player.position; 
        [self addChild:sp z:10]; 
        CGPoint vel = hudLayer.rightJoystick.velocity; 
        vel = ccpMult(ccpNormalize(vel), 1000); 
        [sp runAction:[CCMoveBy actionWithDuration:3 position:vel]];
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜