Need help! Setintervals!

Started by: chahn375 | Replies: 7 | Views: 697

chahn375

Posts: 0
Joined: Sep 2025
May 17, 2008 3:55 AM #136481
Yeh I'm new to flash, but i do java coding so i know most syntax already, and i could just be being stupid, but i really dont see how my code doesnt work. every time the key 'a' is pressed, the MC assassinsrush is supposed to play, and you are not supposed to be able to us it for 3 seconds. unfortunately, at the moment, it just keeps going, and it is quite frustrating. help plox? code below, credit for heloping me make this goes to Rather Cheesy, for his platformer tutorial.

onClipEvent (load) {

y = jump=0;
speed = 5;
midair=false;
canmove=true;
rush=true;
}
//make the variable "speed" start as 5
onClipEvent (enterFrame) {
if (Key.isDown(Key.CONTROL)) {
this.gotoAndStop(3);
speed = 15;
//if you press CTRL make the player MC stop at the third frame and the speed = 10
} else {
speed = 5;
//if CTRL is not down speed is still 5
}
}
onClipEvent (enterFrame) {
//the jumping code is hard to explain but it works
xmax = this.getBounds(_root).xMax;
xmin = this.getBounds(_root).xMin;
ymax = this.getBounds(_root).yMax;
ymin = this.getBounds(_root).yMin;
if (canmove&&Key.isDown(Key.RIGHT) && !_root.ground.hitTest(xmax+speed, _y, true)&&!_root.wall.hitTest(xmin+speed+35, _y, true)) {
_x += speed;
_xscale = 100;
//when the right arrow is down go to the right at a speed of 5 and face right
}
if (canmove&&Key.isDown(Key.LEFT) && !_root.ground.hitTest(xmin-speed, _y, true)&&!_root.wall.hitTest(xmin-speed-35, _y, true)) {
_xscale = -100;
_x -= speed;
//when the left arrow is down go to the left at a speed of 5 and face left
}
if (_root.ground.hitTest(_x, ymax-(y/2), true)) {
jump = true;
y = 0;
} else {
y -= 2;
jump = false;
}
//jumping code^^^
if (jump && Key.isDown(Key.UP)) {
y = 15;
}
_y -= y;
}
//more jumping code but change the 20 here to change his jump height
onClipEvent (enterFrame) {
if (!_root.wall.hitTest(xmin+speed+35, _y, true)&&Key.isDown(Key.UP))
{
this.gotoAndPlay(2);
//when you press space make this go to and stop at the second frame
}
if(!jump)
{
this.gotoAndPlay(2);
}
}
onClipEvent (enterFrame)
{
Timer=new Timer(1500,0);
if(Timer.currentCount%2==0);
{
rush=true;
}
if (!(moving)&&Key.isDown(Key.SHIFT))
{
canmove=false;
this.gotoAndStop(4);
}
if(Key.isDown(65)&&rush)
{
rush=false;
_x+=50;
}
if(!rush)
{
this.gotoAndStop(8);

}
if (!(moving)&&Key.isDown(88)) {
canmove=false;
this.gotoAndStop(5);
//when you press shift make this go to and stop at the fourth frame
}
if(!(Key.isDown(Key.SHIFT)||Key.isDown(88)))
canmove=true;
}

onClipEvent (enterFrame) {
//to be used someday for wallrun


}
chahn375

Posts: 0
Joined: Sep 2025
May 17, 2008 3:56 AM #136483
note* the boolean rush is supposed to monitor whether 3 seconds has passed and whether you CAN rush.
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
May 17, 2008 2:56 PM #136649
First off, welcome to Stick Page Portal!

Here's some helpful tips for when posting on the forums:
  • Surround Actionscript in [*code] and [*/code] tags (but without the *'s). This way it keeps the tabbing intact.
  • Use the edit button at the bottom of your previous post when you just have a small message to tack on


Now to your question.

I cannot find any reference for a "Timer" class in AS2, are you sure it exists? Or are you using a non-standard script library?

Here's how I would handle it:

onClipEvent (load) {
rushwait=0;
}

onClipEvent (enterFrame) {

// ...Snip

if(getTimer()-rushwait>=3000);
{
rush=true;
}

//...Snip

if(Key.isDown(65) && rush)
{
rush=false;
_x+=50;
rushwait=getTimer();
}
if(!rush)
{
this.gotoAndStop(8);
}


I cut out the parts that weren't relevant to your specific question, hopefully it's fairly obvious what replaces what.

Anywho, here's how it works: when the player uses their "rush" ability, we set their 'rushwait' variable to the number of milliseconds the flash has been running (which is what getTimer() returns). Now, to check when three seconds have passed, we only need to check that flash has been running for 3000 milliseconds more than the last time we checked, thus 'getTimer()-rushwait>=3000'.

I didn't bother testing it, since I have to go to work, but I'm 98% confident that's what you need. If you have any problems with it, want something else explained, or have another question, post it and I'll try to get to it when I get back.
chahn375

Posts: 0
Joined: Sep 2025
May 19, 2008 10:09 PM #138271
It doesnt work. i put it in exactly. and i meant that rush=true after every 3 seconds. so i changed your gettimer()/3000 to gettimer()%3000.
its still doesnt work though. I DONT GET IT. please test it. and if it works, plz help me.
Rather Cheesy
2

Posts: 2,137
Joined: Apr 2007
Rep: 10

View Profile
May 19, 2008 10:18 PM #138279
Wow, my old code is messy... >_>

In order to make a "rush" last for 3 seconds, I personally would create MC that lasts for 3 seconds, place it on the char, tell it to play when rush is played, and at the end of that MC add the code that tells rush to turn off. I don't know the exact codes and I don't have time to write them right now. But if you have previous coding experience you should be able to figure it out.
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
May 20, 2008 5:07 AM #138502
Quote from chahn375
It doesnt work. i put it in exactly. and i meant that rush=true after every 3 seconds. so i changed your gettimer()/3000 to gettimer()%3000.
its still doesnt work though. I DONT GET IT. please test it. and if it works, plz help me.


... *Strangles*

*cough* Sorry, I'm a bit tired.

Did you read my post? There's not a single bit of division in it. Also, do you know what "%" does? "%" is the modulus operator; it returns the remainder of the division of two variables, not the quotient itself. So "7%3" would return 1, since 7/3 is 2 Remainder 1. "getTimer()%3000" would have no valuable meaning in this context.

Post what your code is now, I'll try to see what isn't working. Alternatively, you can try Rather Cheesy's method, which will work if you can set it up right.
chahn375

Posts: 0
Joined: Sep 2025
May 20, 2008 4:43 PM #138686
yes i know what % does. i know that you dont have division i nyours, i realized that after i posted. and what im trying to make it do is make it so that the user may rush only every 3 seconds, not after 3 seconds after the MC loads. so i assumed that every multiple of 3000ms from the timer would work and make it go every 3 seconds. here is my current code:

onClipEvent(load)
{
if((getTimer()%3000)==0)
{
this.rush=true;
}
}
onClipEvent (enterFrame)
{
rush=true;

function rushable()
{
rush=true;
}

if(Key.isDown(65) && rush)
{

if(xscale==-100)
{
_x-=50;
}
else
_x+=50;
rush=false;
}

if(!rush)
{
this.gotoAndStop(8);
}
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
May 20, 2008 10:22 PM #138800
The way I would do this is that when the rush is used it starts/resets a timer and while the time is '>0' rush can't happen. When it gets to 0 or less it can happen, just remember to change the timer to 0 if it goes under. Can't be bothered typing the code right now but you get the idea. Alot simpler than what your trying to do, I think.