View Full Version : Fire delay - Shooter game
NewToFlash
November 7th, 2004, 09:14 AM
Hey, I'm pretty new to actionscript and so but I manage to read and understand and alter other people's code a little. I'm testing functions to make myself a First Player Shooter game and now I have problem with the fire delay. I want a limit of let's say 1 shot per second. How do I do that?
I thought that maybe restricting the clickings counted, so it doesn't register the mouse clicks too close to eachother. Or put some kind of interval at the functions delaying the sound and event function, but that seems too hard.
So... any ideas?
And do you know any good help files for making this kind of game?
-----
The game so far: http://w1.470.telia.com/~u47035396/flash/game/game.htm
As u see one get multiple sounds when clicking a lot and it's possible to get several points by clicking the ball a lot.
Also, when the ball goes behind the tree it's not supposed to be able to shoot.
The Flash File: http://w1.470.telia.com/~u47035396/flash/game/ball.fla
It's not the exact same, some junk code trying to make the delay...
----
The file I used for help (if someone finds it useful for making a game of their own ;) http://www.flashkit.com/movies/Games/Components/Simple_s-Stefan_B-1407/index.php
Here u see the target isn't shot behind the pics. Is that because it is a button there and a mc in my file?
---
Thanks in advance :)
NewToFlash
November 7th, 2004, 01:12 PM
I came up with this with the help of a guy who tried to help me in chat... it is not working though. Maybe that could be the way to fix it if I get it working??
stop();
//;
i = 0;
hinna.onMouseDown = function() {
if (i=0) {
pang = new Sound(this);
pang.attachSound("shotgun");
pang.setVolume(40);
pang.start();
}
};
i++;
if (i == 12) {
i = 0;
}
murka390
November 7th, 2004, 02:08 PM
of course there may be other and easyer methods, but this code should help you solve your problem.
shoot_allowed = true;
/*shooting is allowed*/
n = 0;
m = 0;
/*some helpful variables*/
YourMovieClipName.onMouseDown = function() {
if (shoot_allowed == true) {
/*other actions here*/
n += 1;
trace("Bang nr."+n+"!");
shoot_allowed = false;
/*if one shot is done, you cant make another until shoot allowed is "true" again*/
myInterval = setInterval(allow_shoot, 1000);
/*sets the interval of allowing another shot*/
allow_shoot();
/*calls the function that allows another shot*/
} else {
trace("NO SHOT ALLOWED!");
}
};
allow_shoot = function () {
/* function that allows another shot*/
if (m>=2) {
/* this is here because,it does'nt allow the shot*/
/* right when this function is called*/
shoot_allowed = true;
/* this allows another shot*/
clearInterval(myInterval);
/* this clears the interval, that allows the shot*/
m = 0;
} else {
m += 1;
/* this "m" is used for this purpose: when the allow_shot function is called, it adds 1 to m(with interval of 1000ms), and when m is equal or bigger then 2, it allows the shot*/
}
};
Nominator
November 7th, 2004, 02:11 PM
I thin those two // are making the whole code a comment
NewToFlash
November 7th, 2004, 05:36 PM
Thanks alot mate!! This is what makes me understand AS better =)
Now I just have to fix some extra things with the help of this code :)
NewToFlash
November 8th, 2004, 12:45 PM
The pang stuff I added worked perfectly, but not the other stuff I tried to add. I also put an extra if down there, don't know if it should be there.
shoot_allowed = true;
n = 0;
m = 0;
hinna.onMouseDown = function() {
if (shoot_allowed == true) {
// other actions here
//
pang = new Sound(this);
pang.attachSound("shotgun");
pang.setVolume(40);
pang.start();
//
mål.boll.onPress = function() {
if (shoot_allowed == true) {
newscore = Number(score)+5;
score = newscore;
/:score = newscore;
mål.boll.explode.gotoAndPlay(2);
hit = new Sound(this);
hit.attachSound("wreee");
hit.start();
}
//
};
//
n += 1;
trace("Bang nr."+n+"!");
shoot_allowed = false;
myInterval = setInterval(allow_shoot, 1000);
allow_shoot();
} else {
trace("NO SHOT ALLOWED!");
}
};
allow_shoot = function () { if (m>=2) {shoot_allowed = true;clearInterval(myInterval);m = 0;} else {m += 1;}};
Don't know how to make such a nice code holding thingy that u made :P
murka390
November 8th, 2004, 01:00 PM
is "boll" a button or a movieClip?
try it like this:
shoot_allowed = true;
n = 0;
m = 0;
mål.boll.onPress = function() {
if (shoot_allowed == true) {
shoot_allowed = false;
pang = new Sound(this);
pang.attachSound("shotgun");
pang.setVolume(40);
pang.start();
score += 5;
// adds 5 to score
mål.boll.explode.gotoAndPlay(2);
hit = new Sound(this);
hit.attachSound("wreee");
hit.start();
n += 1;
trace("Bang nr."+n+"!");
myInterval = setInterval(allow_shoot, 1000);
allow_shoot();
} else {
trace("NO SHOT ALLOWED!");
}
};
allow_shoot = function () {
if (m>=2) {
shoot_allowed = true;
clearInterval(myInterval);
m = 0;
} else {
m += 1;
}
};
NewToFlash
November 8th, 2004, 01:09 PM
But there is a difference. hinna.onMouseDown calls the sound of the gun both regardless if it hits or misses. mål.boll.onPress is the mc that moves around. When it is hit there should be a sound and the score goes up.
If I'm not mistaken the fire sound now only comes when I hit the target and not if I miss.
NewToFlash
November 8th, 2004, 01:20 PM
Both hinna and boll are MCs.
Maybe I should have the target (boll) as a button to prevent shooting "through" objects like trees?
murka390
November 8th, 2004, 01:34 PM
if the tree is a movieclip, it does'nt matter if "boll" is a button or a movieclip, it still shoots it. tree must be a button, and the layer of the tree must be higher than the "boll" layer.
this should work... if you have instance names and reffering correct.
if the "boll" is a button, then use .onPress, but if it's a movieClip, use .onMouseDown.
shoot_allowed = true;
n = 0;
m = 0;
hinna.onMouseDown = function() {
if (shoot_allowed == true) {
pang = new Sound(this);
pang.attachSound("shotgun");
pang.setVolume(40);
pang.start();
mål.boll.onMouseDown = function() {
score += 5;
mål.boll.explode.gotoAndPlay(2);
hit = new Sound(this);
hit.attachSound("wreee");
hit.start();
trace("hit!");
};
n += 1;
trace("Bang nr."+n+"!");
shoot_allowed = false;
myInterval = setInterval(allow_shoot, 1000);
allow_shoot();
} else {
trace("NO SHOT ALLOWED!");
}
};
allow_shoot = function () {
if (m>=2) {
shoot_allowed = true;
clearInterval(myInterval);
m = 0;
} else {
m += 1;
}
};
NewToFlash
November 8th, 2004, 02:12 PM
Nope =/
With the mål.boll.onMouseDown = function() the ball explodes and makes sound no matter where I click. It only notice if I click or not, not where I click.
murka390
November 8th, 2004, 02:24 PM
maybe it would be better if you send your .fla
so i could look it all over.
NewToFlash
November 8th, 2004, 02:28 PM
http://w1.470.telia.com/~u47035396/flash/game/ball.fla
Here, and thanks for the effort to make this work. I appreciate it =)
murka390
November 8th, 2004, 06:22 PM
here you go:
http://www.hot.ee/murka390/ball.fla
i took the interval off and added the true and false variables to timelines
(when you hit the ball, the shoot_allowed = false, and when warte2 == warte
then shoot_allowed = true)
i changed the tree to a button, and everything seems to work now.
NewToFlash
November 9th, 2004, 11:35 AM
Thanks for the help murka ;)
I had to do a few changes because it was possible to multi fire half of the time so I moved the true/false variables to a seperate timeline and so. But thanks again, you've been more than helpful :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.