Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Shooting problems in 2D side scroller

Author
Message
Duncan161
13
Years of Service
User Offline
Joined: 15th Mar 2011
Location:
Posted: 3rd May 2011 15:16
I was wondering if anyone could help me with a problem I've been having. Basically I'm trying to make a stationary turret shoot in the direction of the mouse but Im having some issues. At the moment nothing seems to happen and I don't know if this is because of an error I've made or the round about way I have written the code.
Sorry about the poor quality of the code but if anyone can help that would be great


Set Display Mode 800,600,16
Sync on
Ink 0,RGB(0,0,0)
Do

Load Image "C:\Documents and Settings\20132450\My Documents\My Pictures\turret.bmp",1
Load Image "C:\Documents and Settings\20132450\My Documents\My Pictures\bullet.bmp",2

Sprite 1,0,250,1
Sprite 2,0,250,2

Xp#=Sprite X(2)
Yp#=Sprite Y(2)

Mx#=MouseX()
My#=MouseY()

If MouseClick()=1
shooting=1
endif

If shooting=1
Xp#= Xp#+100
Yp#= Yp#+100
endif

Counter=0

If Xp#=Mx#
Counter= Counter + 1
endif

If Yp#=My#
Counter= Counter + 1
endif

If MouseClick()=0
Counter= Counter + 1
endif

If Counter=3
shooting=0
endif

CLS RGB(255,255,255)
sync
loop
end
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 3rd May 2011 22:23
First thing, it's good practice to put your code in a code box, that way you don't lose any formatting (i.e. indenting). In the message window, highlight your code, press the code button in the top right of the message window.

Ok, looking through your code I'm seeing quite a few issues.

Probably the most fundamental is that you're not changing the position of the bullet sprite at all.

You need to use the Xp# and Yp# variable to position the sprite.

The other issue you have is that the x and y velocities of the bullet do not change, so the bullet will always move off in the same direction and speed. I use the term velocity in it's strictest sense.

Now, to get you out of having to use trig we can deal with the x and y velocities seperately. Yes, we can do that, I've been a mechanical engineer for ten years so trust me (but I'm nothing like Wollowitz off the Big Bang Theory before you ask).

A velocity has both magnitude and direction. The magnitude is just how fast something is moving (i.e. it's speed) and the direction is whether it's going to be a positive or negative value.

The direction of the x velocity can be fixed as you have placed the turret on the far left of the screen. Therefore the x velocity is always going to be positive (i.e pointing to the right), you can then set the value as a constant at the start of the program if you want.

The y velocity is going to change, in its direction (i.e. moving the bullet up the screen or down the screen) and in its maginatude, otherwise the bullet will always move off at the same angle (positive or negative).

In the demo the y velocity of the bullet is calculated using the principle of simillar triangles. I.e. the position of the mouse relative to the... it's easier if I just attached a picture.

The demo below shows the basic principle or how to get the bullet to move. I've taken out the bullet stopping if you realese the mouse button but the bullet position will reset if goes of the screen. I've also created a couple of sprites in the code so you won't require any media.


If you wanted the turret to move, i.e. have it as a tank, then you'll need to calculate the x velocity of the bullet based on the tanks position to the mouse.

You can calculate the velocity of the bullet using trig but if you don't know your sines from your cosine it can be a little tricky to pick up.

Attachments

Login to view attachments
Duncan161
13
Years of Service
User Offline
Joined: 15th Mar 2011
Location:
Posted: 9th May 2011 16:13
Thanks that was a great help. Sorry about the code I thought Id put it in a box :S

Login to post a reply

Server time is: 2024-11-16 21:56:50
Your offset time is: 2024-11-16 21:56:50