Hello all,
I'm an iPhone developer but new to games development and cocos2d, even newer then to Box2d.
I have been following an example from http://www.uchidacoonga.com/2011/03/simple-platformer-using-cocos2d-and-box2d-with-collision-detection/ - From this I have been able to set up a very basic world and I have a hero I can move.
I integrated a sneakyinput d-pad - Just from the example code that came with sneaky - this may be a mistake (I'll get to the problem.)
So at the moment I have a very basic game where I can move my hero forward, back and jump usung the d=pad and the jump button.
The problem here now is that this is only working if I 'tap' the d-pad, if I hold it down forward my hero will 'run away' and while I'm holding the d-pad the jump button is not responding to input.
I have a GameScene - main world where I load my hero into. I also load another class 'JoystickLayer' that sits on top to grab all touches. The sneakyinput joystick and buttons all reside on this layer. I have modelled this pretty much from another game I made that didn't use any physics. I listen for input in the 'update' function:
Then the hero is doing its own moving:
Using the above code I can somewhat move the hero but it's a small bit difficult and it's not as smooth as one would hope. I'm obviously doing it wrong. I have been searching for tutorials covering sneakyinput and box2d but I haven't come across any that would apply here.
Could someone please be so kind and point out where I'm going wrong here?
I'm an iPhone developer but new to games development and cocos2d, even newer then to Box2d.
I have been following an example from http://www.uchidacoonga.com/2011/03/simple-platformer-using-cocos2d-and-box2d-with-collision-detection/ - From this I have been able to set up a very basic world and I have a hero I can move.
I integrated a sneakyinput d-pad - Just from the example code that came with sneaky - this may be a mistake (I'll get to the problem.)
So at the moment I have a very basic game where I can move my hero forward, back and jump usung the d=pad and the jump button.
The problem here now is that this is only working if I 'tap' the d-pad, if I hold it down forward my hero will 'run away' and while I'm holding the d-pad the jump button is not responding to input.
I have a GameScene - main world where I load my hero into. I also load another class 'JoystickLayer' that sits on top to grab all touches. The sneakyinput joystick and buttons all reside on this layer. I have modelled this pretty much from another game I made that didn't use any physics. I listen for input in the 'update' function:
Code:
-(void) update:(ccTime)delta
{
GameScene* game = [GameScene sharedGameScene];
Player* player = [game defaultPlayer];
if (jumpButton.active)
{
[player jump];
// this doesn't happen if the d-pad is held
}
CGPoint velocity = ccpMult(joystick.velocity, 200);
if (velocity.x != 0 || velocity.y != 0)
{
[player move:velocity];
// When D-Pad is held this is called every frame
}
}
{
GameScene* game = [GameScene sharedGameScene];
Player* player = [game defaultPlayer];
if (jumpButton.active)
{
[player jump];
// this doesn't happen if the d-pad is held
}
CGPoint velocity = ccpMult(joystick.velocity, 200);
if (velocity.x != 0 || velocity.y != 0)
{
[player move:velocity];
// When D-Pad is held this is called every frame
}
}
Then the hero is doing its own moving:
Code:
-(void) move:(CGPoint)velocity {
b2Vec2 impulse;
if (velocity.x > 0) {
impulse = b2Vec2(1.0f, 0.0f);
}
else if (velocity.x < 0) {
impulse = b2Vec2(-1.0f, 0.0f);
}
body->ApplyLinearImpulse(impulse, body->GetWorldCenter());
}
b2Vec2 impulse;
if (velocity.x > 0) {
impulse = b2Vec2(1.0f, 0.0f);
}
else if (velocity.x < 0) {
impulse = b2Vec2(-1.0f, 0.0f);
}
body->ApplyLinearImpulse(impulse, body->GetWorldCenter());
}
Using the above code I can somewhat move the hero but it's a small bit difficult and it's not as smooth as one would hope. I'm obviously doing it wrong. I have been searching for tutorials covering sneakyinput and box2d but I haven't come across any that would apply here.
Could someone please be so kind and point out where I'm going wrong here?