ForumsProgramming ForumBest way to do weapons

6 3433
herofmb
offline
herofmb
30 posts
Nomad

Hey guys, I'm making a shooting game and I need to add some delay between shots. I did in a way, but I think it's a bit ugly and not much efficient, this is the code for a machine gun weapon, wich you have a 0.5 delay between each shoot (the code works, i just want your opinion about it):


public class Mp5 extends Weapon
{
//var to check if mouse is down
private var private var mouseDown:Boolean = false;
//the timer for the delay
private var timer:Timer = new Timer(100,5);
//the var to check if timer has finished
private var timerFinished:Boolean = true;

public function Mp5():void
{
// events to handle mouse pressed
Game.main.stage.addEventListener(MouseEvent.MOUSE_UP, stopShooting);
Game.main.stage.addEventListener(MouseEvent.MOUSE_DOWN, startShooting);

//enterFrame to check the boolean
//variables and shoot
//acording to it's value
this.addEventListener(Event.ENTER_FRAME, shoot);
}

public function startShooting(e:MouseEvent):void
{
mousePressed = true;
}

public function stopShooting(e:MouseEvent):void
{
mousePressed = false;
}

//HERE IS MY DOUBT IF THIS CODE IS OK OR NOT
public function shoot(e:Event):void
{

if(mousePressed && timerFinished)
{
//THIS IS JUST TO CREATE
//THE BULLET, DOESN'T
//MATTER FOR THIS THREAD
var a:Number = 85*Math.cos((this.rotation*grausParaRadianos));
var b:Number = 85*Math.sin((this.rotation*grausParaRadianos));
var bullet:Bullet = new Bullet( (parent.x + this.x+a) , (parent.y + this.y+b), this.rotation);
Game.main.addChild(bullet);
somDoTiro.play();
//HERE I SET THE VARIABLE
//TO FALSE
timerFinished = false;
}
// HERE IS WHERE WE START THE TIMER
//AND ADD THE EVENT
// THE GUN WILL ONLY SHOOT IF THE
//EVENT HAS FINISHED
timer.start();
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

}

//SETTING UP THE VARIABLE AFTER THE TIMER
//HAS FINISHED
private function onTimerComplete(event:TimerEvent):void
{
timerFinished = true;
}

}

Thanks to all

herofmb

  • 6 Replies
herofmb
offline
herofmb
30 posts
Nomad

OMG sorry again aboout the title, It should be

"Best way to do weapons delay between each shoot"

dank
offline
dank
983 posts
Peasant

Well, on the subject of code optimization, I would suggest first that you create a public, static class that contains all of your control values. This way you can have a single mouse and key event to handle all the inputs instead of recreating them in every object. Here's a package similar to what I make in most of my games:

package dank.common {

import flash.display.*;
import flash.events.*;

public class Controls{

// Associative Array
public static var keys:Array = new Array();

public function Controls(){
//// NOTE that all events listeners are instantiated in the document root
keys["MOUSE"] = false;
keys["KEY_A"];
}


public static function mouseD(e:MouseEvent){keys["MOUSE"] = true;}
public static function mouseU(e:MouseEvent){keys["MOUSE"] = false;}

public static function get key():Array{ return keys;}

// Key Down Event
public static function keyD(e:KeyboardEvent){
switch(e.keyCode){
case 65: keys["KEY_A"] = true;
break;
}

}

// Key Up Event
public static function keyU(e:KeyboardEvent){
switch(e.keyCode){
case 65: keys["KEY_A"] = false;
break;
}

}

}
}


You can then go on to import the package and use a simple call to the static class like Controls.keys.KEY_A and that will return the value of that key.

As for polymorphism, I would suggest putting the shoot function in your Weapon class as surely the MP5 won't be the only weapon that shoots. However, if you have melee weapons, I would make a new class for shooting weapons. In there your should have the function to shoot and variables that only the guns have like ammo, rate of fire, etc., then your MP5 should inherit that class and you melee weapons inherit the weapon class.

And lastly for the timer, I see no problem with your means, though I would personally use a step counter. The only reason to use the timer is if you don't want to figure out how many steps are equivalent to some number of seconds. I believe it may be more efficient, though I don't see it causing any serious delays as far you should be concerned.
herofmb
offline
herofmb
30 posts
Nomad

Thanks, I'll certainly use those tips. I already have a class named "Keys", but I wasn't using it much cause everytime I wanted to use I had to make a Keys object. With static functions and atributes, will be much better to handle all those events.

Really useful package I'll certainly use it.

For the polymorphism, the shoot method is already in the Parent class and I override it in the Mp5 class, I just took it off to explain better. Anyway the tips are still very useful.

And for the timer, I'll check it out the step counter, since I didn't knew it.

Hope soon I can show some of my work.

Cya

herofmb

herofmb
offline
herofmb
30 posts
Nomad

Hey Dank, I have a question for you. I'm now using the static class to manage all my keyboard and mouse events.

But this is good when you need to set up a variable (the associative array)with true or false and using this values to, for example, move your character while the variable is set to true.

Altough what should I do if I want to call a function just after an event like KEY_DOWN happened? To make myself clear, I have a function in the Hero class wich makes him switch weapons according to the key pressed. If I press "1" it switches to pistol, for instance.

What I was doing before is creating an event of type KeyboardEvent.KEY_DOWN inside the Hero class and make it calls the function to switch weapons.

But we all don't want a lot of events, to optimization, so What I'm doing is make all those functions in the Hero class of type static and calling them direct from the static class.

Do you think this is a good idea?

Regards,

herofmb

dank
offline
dank
983 posts
Peasant

I would just have a switcher variable that turns on when the key is down and off when the key is released that way the function will only be called once. I would also check if the weapon being changed to is not the same as the current weapon.

herofmb
offline
herofmb
30 posts
Nomad

Reviving the topic:

Dank, about your last reply: My question is when to test for the switcher variable. If I just set a variable, let's say a keys["1"], using the associative array, I'll have to test if it's on to change weapons, and while it is on it will keep changing the weapon (because i don't know how to do in any other way than keep testing on the ENTER_FRAME event that moves the hero). So it doesn't look much eficient to me.
What I was doing is make all those functions in the Hero class of type static and calling them direct from the static class.

Give me a light ^^

herofmb

Showing 1-6 of 6