I want that my sprite go always forward, and you can only control his direction moving right and left (on 360 degrees). I don't know why, but the movement it's senseless.
The constants to move and rotate:
const int POD_DEGRE_MOVE = 5;
const int POD_STEP_MOVE = 3;
The sprite initalization:
_dragonSprite->setPosition(Point(visibleSize.width / 2,_dragonSprite->getContentSize().height * 0.75));
addChild(_dragonSprite, 2);
The event key listener:
void GameScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event *event){
_pressedKey = keyCode;
switch (_pressedKey) {
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
giro = -POD_DEGRE_MOVE;
_isMoving = true;
break;
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
giro = POD_DEGRE_MOVE;
_isMoving = true;
break;
}
}
void GameScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event *event){
if (_pressedKey == keyCode) {
_pressedKey = EventKeyboard::KeyCode::KEY_NONE;
_isMoving = false;
giro = 0;
}
}
And the calculations of the new direction:
void GameScene::update(float dt) {
_dragonSprite->setRotation(_dragonSprite->getRotation() + giro);
_podVector = Vec2(cos(_dragonSprite->getRotation()) * POD_STEP_MOVE, sin(_dragonSprite->getRotation()) * POD_STEP_MOVE);
Vec2 newPos = Vec2(_dragonSprite->getPosition().x + _podVector.x, _dragonSprite->getPosition().y + _podVector.y);
_dragonSprite->setPosition(newPos);
}
Another problem is that the sprite at the start moves to the right (because Cos(0)==1), so I rotate the image of the sprite 90 degrees to the right, to then rotate it again at the initialization 90 to the left so the sprite is in the right position and his start angle is 90 (sin(90)==1), but the angle is not 90, and it moves as he wishes.
The last problem I have with this is that when I press a key (right or left), the sprite stops, rotates, and starts again (a direction that is not the direction where the sprite is aiming, that's the main problem). The sprite shouldn't stop while you are moving it.
Can anyone help me?