Page 1 of 2

Top down shooter (4 way shooting problem)

PostPosted: Thu Mar 22, 2012 10:39 pm
by Johnno
Hello everyone,

I having some problems with code, I need to set up a variable called direct and attached that to the "GetKeyState" function to make my character shoot 4 ways.

I using the following code, but It's not working.

Code: Select all
int direct;

char*key=GetKeyState();
if(key[KEY_LEFT] == 1)direct = 0;
if(key[KEY_RIGHT] == 1)direct = 1;
if(key[KEY_UP] == 1)direct = 2;
if(key[KEY_DOWN] == 1)direct = 3;


then in my bullet actor

Code: Select all
if(direct==0) // left
{CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->xvelocity=-10;}
if(direct==1) // right
{CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->xvelocity=10;}
if(direct==2) // up
{CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->yvelocity=-10;}
if(direct==3) // down
{CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->yvelocity=10;}


What am I doing something wrong?

Thanks for any help
Johnno

PS. added my game files

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 12:21 am
by SuperSonic
Johnno wrote:I using the following code, but I'm getting errors.
Could you post the errors you're getting please? :D

Also, instead of using if(key[KEY_LEFT] == 1)direct = 0; Try doing this instead: if(key[KEY_LEFT])direct = 0; (just take out that "== 1" part) :wink:

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 12:38 am
by Johnno
Hmm, for some reason I changed something and the errors went away, but it's still not working. I attached the ged. files, see first post.

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 12:47 am
by skydereign
The problem is you are declaring the int direct as a local variable. If you want to have the value of direct to be usable in both events, you need to make direct global, and remove the int direct at the beginning of the draw event. Also I'd recommend continuing to use (key[KEY_RIGHT]==1) as it is more explicit. In some cases the variable only has an on off values, and so dropping the comparison works, but when people get in the habit of doing that, they can end up causing themselves a bit of problems. Whereas if you keep the ==1, you know exactly what you mean when reading it.

-Edit
Well it seems in your file you fixed that. But, the problem you are having is that you can't use global code that way. Global code is only for declarations, so creating variables and functions. You would need to put that code into the player's draw event (for instance) to get the code to run.

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 1:22 am
by Johnno
So what your saying is remove

Code: Select all
char*key=GetKeyState();
if(key[KEY_LEFT] == 1)direct = 0;
if(key[KEY_RIGHT] == 1)direct = 1;
if(key[KEY_UP] == 1)direct = 2;
if(key[KEY_DOWN] == 1)direct = 3;


from the global script and place it in a "Draw Actor" event on the player. Will give it a go.
Ta

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 2:23 am
by Johnno
Well I must be doing something really wrong here. I think the code is solid I just need to put it it the right place.

My latest .ged file is attached if anyone has the time to look at it. It seams to be a recurring problem here which I can't find a post that address it.

The bullet will fire but only left (being the first bit of code in Player Key Down) This is want I've done.

Global Script
Code: Select all
int direct;


Player - Key down
Code: Select all
if(direct==0) // left
{CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->xvelocity=-10;}
if(direct==1) // right
{CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->xvelocity=10;}
if(direct==2) // up
{CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->yvelocity=-10;}
if(direct==3) // down
{CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->yvelocity=10;}


Player - Draw Actor
Code: Select all
char*key=GetKeyState();
if(key[KEY_LEFT] == 1)direct = 0;
if(key[KEY_RIGHT] == 1)direct = 1;
if(key[KEY_UP] == 1)direct = 2;
if(key[KEY_DOWN] == 1)direct = 3;

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 2:27 am
by skydereign
I mentioned this in my first post, the problem is you are making a local variable called direct, which takes precedence over your global variable direct. Remove the int direct; line from the player's draw actor. That line makes a temporary copy of the variable, meaning when you set direct to another value, it is only setting the temporary one, instead of the global one.

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 2:58 am
by Johnno
Ahh sorry, now I get it (doh!), all working now thanks.

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 10:17 pm
by Johnno
I'm trying to integrate a ammo clip, so the ammo runs out after 5 shots, and a text actor that shows how much you have left.
I can't get it to count down .This is what I've done so far.

Global script
Code: Select all
int clip;


Player - key down
Code: Select all
if (clip !=0)

if(direct==0) // right
{
CreateActor("bullet", "bullet_anim", "(none)", "(none)", 0, 0, false)->xvelocity=10;
clip -= 1;
}


Ammo text counter - create actor
Code: Select all
clip=5;

sprintf(text, "%i", clip);

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 10:54 pm
by skydereign
The problem is that the create actor event only happens once. You should take some time to fully understand when certain events trigger, and why that makes them special/useful. You use the create actor as a way of setting values, since it only triggers when the actor is first created. If you want code to trigger all the time, you can use draw actor. In your case you wanted to set clip to 5 when the actor is created, but really you want to display what clip equal not only on the first frame, but every frame thereafter. So, put the line that displays the variable in the actor's draw event.

Re: Top down shooter (4 way shooting problem)

PostPosted: Fri Mar 23, 2012 11:55 pm
by Johnno
Hey thanks skydereign,

I'm really new to this (first attempt at making a game).

So the countdown now works (thanks) but it doesn't stop firing and the counter goes into minus! lol.

Re: Top down shooter (4 way shooting problem)

PostPosted: Sat Mar 24, 2012 12:17 am
by Johnno
Fixed it! needed to change

Code: Select all
if (clip !=0)


to

Code: Select all
if (clip >0)

Re: Top down shooter (4 way shooting problem)

PostPosted: Sat Mar 24, 2012 12:31 am
by Johnno
ok so that didn't work, as soon as I move the player it starts shooting again!

Its that events/trigger thing isn't it?

Re: Top down shooter (4 way shooting problem)

PostPosted: Sat Mar 24, 2012 12:40 am
by skydereign
The code that should have an if statement like that would be the fire key, right? It makes sense to only fire bullets if ammo is greater than zero.

Re: Top down shooter (4 way shooting problem)

PostPosted: Sat Mar 24, 2012 12:57 am
by Johnno
Yeah, I have it in my fire key (space bar). The interesting thing is, it stops firing left but not he other directions. Do i need to add (if (clip >0) // ammo) above each firing direction?

player - key down - space bar - script
Code: Select all
if (clip >0) // ammo

if(direct==0) // left

{
CreateActor("dart", "dart_left", "(none)", "(none)", 0, 0, false)->xvelocity=-15; //fire left
clip -= 1; // take 1 ammo away
}

if(direct==1) // right

{
CreateActor("dart", "dart_right", "(none)", "(none)", 0, 0, false)->xvelocity=15; //fire right
clip -= 1; // take 1 ammo away
}

if(direct==2) // up

{
CreateActor("dart", "dart_up", "(none)", "(none)", 0, 0, false)->yvelocity=-15; //fire up
clip -= 1; // take 1 ammo away
}

if(direct==3) // down

{
CreateActor("dart", "dart_down", "(none)", "(none)", 0, 0, false)->yvelocity=15; //fire down
clip -= 1; // take 1 ammo away
}