Top down shooter (4 way shooting problem)

Game Editor comments and discussion.

Top down shooter (4 way shooting problem)

Postby Johnno » Thu Mar 22, 2012 10:39 pm

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
Attachments
topdown.zip
(798.78 KiB) Downloaded 16 times
Last edited by Johnno on Fri Mar 23, 2012 12:34 am, edited 1 time in total.
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby SuperSonic » Fri Mar 23, 2012 12:21 am

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:
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1444
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Fri Mar 23, 2012 12:38 am

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.
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby skydereign » Fri Mar 23, 2012 12:47 am

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.
User avatar
skydereign
 
Posts: 3443
Joined: Mon Jul 28, 2008 8:29 am
Score: 569 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Fri Mar 23, 2012 1:22 am

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
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Fri Mar 23, 2012 2:23 am

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;
Attachments
TopDown.zip
(803.91 KiB) Downloaded 13 times
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby skydereign » Fri Mar 23, 2012 2:27 am

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.
User avatar
skydereign
 
Posts: 3443
Joined: Mon Jul 28, 2008 8:29 am
Score: 569 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Fri Mar 23, 2012 2:58 am

Ahh sorry, now I get it (doh!), all working now thanks.
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Fri Mar 23, 2012 10:17 pm

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);
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby skydereign » Fri Mar 23, 2012 10:54 pm

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.
User avatar
skydereign
 
Posts: 3443
Joined: Mon Jul 28, 2008 8:29 am
Score: 569 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Fri Mar 23, 2012 11:55 pm

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.
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Sat Mar 24, 2012 12:17 am

Fixed it! needed to change

Code: Select all
if (clip !=0)


to

Code: Select all
if (clip >0)
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Sat Mar 24, 2012 12:31 am

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?
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby skydereign » Sat Mar 24, 2012 12:40 am

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.
User avatar
skydereign
 
Posts: 3443
Joined: Mon Jul 28, 2008 8:29 am
Score: 569 Give a positive score

Re: Top down shooter (4 way shooting problem)

Postby Johnno » Sat Mar 24, 2012 12:57 am

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
}
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Next

Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest