Hi, I´m making a part of game where player is able to shoot. Now, when player shoots, bullet flies just to mouse cursor and then stops. I need the bullet to continue straight and not to stop when it reaches the cursor - but I have absolutely no idea how to do that... :-\
Here is the code I have for shooting now:
if (Mouse.Mode== eModeShoot && button == eMouseLeft && GetRoomProperty("Battle")== 1) {
if (!IsTimerExpired(1)) return;
else {
SetTimer(1, invGun.ItemAtIndex[0].GetProperty("ShootingSpeed"));
player.FaceLocation(mouse.x, mouse.y, eBlock);
cBullet.ChangeRoom(player.Room, player.x, player.y);
player.LockView(PISTOL);
player.StopMoving();
player.Animate(player.Loop+4, 7, eOnce, eNoBlock);
cBullet.Move(mouse.x, mouse.y, eNoBlock, eAnywhere);
btnBullet_portrait.Visible=false;
SetTimer(20, 14);
}
}
Any suggestions?
Define int xDest and int yPos.
Depending on which way the player faces, set xDest to 0 (when facing left) or whatever the width of your game's resolution is (when facing right).
Set yPos to the value you need (possible half the height of the player).
Then
cBullet.Move(xDest, yPos, eBlock, eAnywhere)
That way you have the bullet travel at a fixed "height" to either end of the screen. If the sprite is somewhat large, you may want to add a little space (-10 and screenwidth+10, for example).
That won't do, the bullet is supposed to fly in the direction of the mouse cursor.
A bit of math is needed:
// above on_mouse_click & repeatedly_execute
float bx, by, xd, yd;
bool bif;
// shooting code
player.FaceLocation(mouse.x, mouse.y, eBlock);
cBullet.ChangeRoom(player.Room, player.x, player.y);
bx = IntToFloat(player.x);
by = IntToFloat(player.y);
xd = IntToFloat(mouse.x-player.x);
yd = IntToFloat(mouse.y - player.y);
float l = Maths.Sqrt(xd*xd + yd*yd);
float speed = IntToFloat(invGun.ItemAtIndex[0].GetProperty("ShootingSpeed")); // e.g. 5
xd = speed*xd/l;
yd = speed*yd/l;
player.LockView(PISTOL);
player.StopMoving();
player.Animate(player.Loop+4, 7, eOnce, eNoBlock);
btnBullet_portrait.Visible=false;
bif = true; // send the bullet on its way
// repeately_execute
if (bif == true) {
bx += xd;
by += yd;
cBullet.x = FloatToInt(bx);
cBullet.y = FloatToInt(by);
if (bx < 0.0 || bx > 320.0 || by < 0.0 || by > 200.0) {
cBullet.ChangeRoom(-1);
bif = false;
}
}
Thank you, KhrisMUC, I would never think out something like that! :o You are genius! :)
Ah, sorry- I misread that. An impressive bit of math there, Khris!
Oh, and thanks for your interest, too, Ghost! :)