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.

Dark GDK / Shooting when mouse is clicked

Author
Message
vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 21st Mar 2011 19:40
Hey guys, since im making a shooter game (2D) and i got helped by Hassan (thanks a lot hassan)

but now i got my shooting code (made by hassan)

but it only move the sprite if the mouse button is pressed...

heres the code of shooting:



Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 21st Mar 2011 21:46
Your problem is when you are NOT clicking, it moves the sprite to the player position and only when you ARE clicking, does it have a chance to actually move
Quote: "dbMoveSprite(7, 10);"

change this line

to


that should clear up the problem.

The fastest code is the code never written.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 22nd Mar 2011 14:18 Edited at: 22nd Mar 2011 14:23
actually the point was to shoot when the mouse is released, so we set a variable to true when mouse is clicked, then, if this variable is true, and the mouse is not clicked, that means it has just been released, so simply


and well, since the way GDK handles sprites sucks, or for me at least, i'd make it something like that:


vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 22nd Mar 2011 16:40
thanks all

but it doesnt work...



Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 22nd Mar 2011 16:44 Edited at: 22nd Mar 2011 16:44
if you removed

you shouldn't have done that

and as for

i think the sprite will be fixed in its place like that, make it like


vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 22nd Mar 2011 20:52


maybe im stupid or doing somethin wrong or both, but it doesnt work

Srry guys for the trouble :S ... i read the code xxxxx times and i can't figure out a missing part :/

Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 23rd Mar 2011 14:02 Edited at: 23rd Mar 2011 14:04
ah sry - add

right after

also, maybe speed of 10 is too much (at dbMoveSprite)?

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 23rd Mar 2011 16:21 Edited at: 23rd Mar 2011 16:25
Testing... failed.. do you wanna the hole code? maybe it will help:



its too much for a bulet?, if you think so ok what you recommend? 6, 7?



@EDIT

Wait i change the bools to after the includes then now it shoots, but it sometimes spawn the bullets in other places and not in Player Position..

Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 23rd Mar 2011 17:48
It will never collide with anything (unless the enemy is on top of you) because this:

is inside your firing loop:


The fastest code is the code never written.
vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 23rd Mar 2011 18:21
?
i didnt get your point

Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 23rd Mar 2011 19:03
When you declare in the "firing" sequence
Quote: "MouseClk = false;"
, you will make this occur ONLY ONCE per "click".... That's not a problem except that inside:
Quote: " if ( dbMouseClick ( ) != 1 && MouseClk )"

you have your sprite movement... This IS a problem.
Move :

outside that IF statement and try it.

The fastest code is the code never written.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 23rd Mar 2011 19:07 Edited at: 23rd Mar 2011 19:08
it's outside actually, but the code is blinding (messy crazy spacing)


vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 23rd Mar 2011 19:15
its what i say i must be stupid ur saying that i need to put the Move between the


and



like this:
?

Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 23rd Mar 2011 19:36
no i was just showing Hawkblood that the motion is simulated outside the first if statement.

could you tell me what is the problem exactly? what is happening? and when?

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 23rd Mar 2011 19:42
when i shoot, sometimes the bullet is not spawned at the player position, it is spawned at the middle of the map. And 90% of times the bullet is far from being pointed to mouse pos.. can your code (that one that points the bullet to the mouse x and y) be more accurate? if no i understand.

Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Mar 2011 13:55
maybe
int DX = mx - dbSpriteX( 7 );
int DY = my - dbSpriteY( 7 );
should be
int DX = mx - PlayerX;
int DY = my - PlayerY;
and
ANGLE = dbAtanFull ( mx - dbSpriteX ( 7 ), dbSpriteY( 7 ) - my );
should be
ANGLE = dbAtanFull ( mx - PlayerX, PlayerY - my );

because the sprite position (7) after first shot is far from being where you want it

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 24th Mar 2011 15:50 Edited at: 24th Mar 2011 16:23
it worked !!

Thanks a lot to all!!!!!!!!

and now its more accurate than before.

Thanks again xD

@EDIT
Hassan srry but now the bullet is spawned in the middle of the map...

how to fix it?

code:


Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Mar 2011 17:23
i don't know, but it seems the only way that it could happen is to have PlayerX/Y = middle of screen, use dbText to display their values to see (the bullet should spawn there)

a little note: you could declare and assign playerX and playerY after motion of the sprite, because what happens now is:

loop start
1-get player x and y to use later
2-move the sprite
3-use player x and y
loop end

in 2 the playerX/Y might differ from the sprite position, because you moved the sprite after getting it's position, might not be a major issue, but it's better to have the accurate position for the player

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 24th Mar 2011 18:59 Edited at: 24th Mar 2011 19:04
so i need to declare the PlayerX and Y at the
if ( dbMouseClick ( ) != 1 && MouseClk )
{

right?

@edit i put this:


it worked again thanks

@EDIT 2

Ok hassan now what i really need is that class you told me about, to be able to shoot more bullets
can you point me please?

Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Mar 2011 21:24 Edited at: 24th Mar 2011 21:28
the whole point is: the "int" type can handle a numerical value, so if you want to store some money or score data, an int is fine, but we want to store something else, more complex data, what should we do? define a new "type" which is a class/struct/union


so we want our type to be able to hold a bullet, a simple int wont be enough for sure, so we will create a type that stands for a bullet of yours


simple as that!
now, we want to create a new bullet, just the same as defining an int:



and to initiate it's values:


note that we use the "." (dot) to access it's data

and to update, it is very simple, we just use the values in it to pass for the dbSprite or dbMoveSprite or whatever you want:



but now, we want multiple bullets, so we need some sort of an array, a static array of bullets would be fine (Bullet bullets[100], but it's limited to 100 and its somewhat harder to manipulate, so we are going to use an std::vector:

there, we have a vector (array) of bullets, but it's empty, to add bullets to the vector, we use this method

now a copy of myBullet is added in the vector, how would this help? we will loop through our vector elements and update each one individually, like so:


i think that's quite simple, notes:
the array is dynamic, so we only add the things that we need in it, so when the bullet is outside screen, it's useless and we must erase it from the vector, to do so, we use the erase method (as seen above)

final code would look something like this


notice that this is a very very basic way to do it, if you learn more about classes and pointers you can make it much cleaner, and generally better.
also, GetFreeSpriteID ( ) could look like


vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 24th Mar 2011 21:37 Edited at: 24th Mar 2011 21:41
OK.. i have some things to do:

1- You're damn good teaching. You teach me structs in like 5 minutes. tHANKS

2- i did everything you say but compile log:


Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 25th Mar 2011 08:07 Edited at: 25th Mar 2011 08:10
show me your code, and possibly the error line

also, i believe

the mentioned lines should be flipped:


vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 25th Mar 2011 23:56
the error is at




the shooting code:


i changed and it still got errors...

sorry for the trouble :/

Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 26th Mar 2011 09:32 Edited at: 26th Mar 2011 09:32


shouldn't exist in your code..the variable bShooting shouldn't exist, either
this was an example of updating one bullet

we replaced that with the other method, looping through vector elements...

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 26th Mar 2011 12:15
ok i delete it but now... it doesnt shoot.

Shoot2Code:


All code:






Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 26th Mar 2011 12:20 Edited at: 26th Mar 2011 12:23


put it outside the loop..

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 26th Mar 2011 13:36
it worked thanks a lot

Learning C++
VB6 Advanced: 4/5
VB.NET Advanced: 4/5

Login to post a reply

Server time is: 2024-06-27 21:46:57
Your offset time is: 2024-06-27 21:46:57