View Full Version : [AS2] Problem with key.isDown
Kajotex
06-24-2009, 04:56 PM
Hi there... first post here, dont be so hard on me please :)
Anyway I was about to create my of little minigame. i want to start by just moving a movieclip i called player left and right.
to test if the key presses are being recognized i made up this:
onClipEvent (load) {
player.onEnterFrame = function() {
if (key.isDown(KeyRIGHT)) {
_root.status.text = "bla";
}
};
}
I looked this up somewhere here in the forum IIRC. but it does not work.
I wrote this in the action tag of the player.
Anyone mind helping me with this little step forward? :)
Thanks
Welcome to Actionscript.org Kajotex.
onClipEvent - Anyone mind helping me with this little step forward?
I would suggest getting away from onClipEvent's and coding directly on the main timeline in an actiopnscript only layer.
Maybe this tutorial will help you some...
http://www.actionscript.org/resources/articles/52/1/Building-games-in-flash/Page1.html
You can also read about all those topics from that tutorial further in the Flash Help Docs.
http://www.actionscript.org/forums/showpost.php3?p=894029&postcount=2
Throw a trace in there:
onClipEvent (load) {
trace(this + ' ++ ' + player);
player.onEnterFrame = function() {
trace(KeyRIGHT + ' enterframe ');
if (key.isDown(KeyRIGHT)) {
I think maybe you can't reference player from inside of player. Also, it's kind of unusual to see someone mixing onClipEvent(load) with onEnterFrame, since I think you could use onClipEvent(enterFrame) and onLoad and I'm not sure how well they play together.
Kajotex
06-24-2009, 06:32 PM
Maybe this tutorial will help you some...
http://www.actionscript.org/resources/articles/52/1/Building-games-in-flash/Page1.html
Awrite! That is exactly whatim searching for:)
Thanks.
Thanks to you too rrh, but i dont understand your code yet :D
I just added trace() statements.
Kajotex
06-24-2009, 10:01 PM
Hmm.... yerah but at the time i did not know what trace is ;)
anyway the link helped me much. but...still have a little problem.
function move() {
if (Key.isDown(Key.LEFT)) {
this._x -= 5;
gotoAndStop(10);
}
if (Key.isDown(Key.RIGHT)) {
this._x += 5;
gotoAndStop(20);
}
if (Key.isDown(Key.UP)) {
this._y -= 5;
gotoAndStop(5);
}
if (Key.isDown(Key.DOWN)) {
this._y += 5;
gotoAndStop(15);
}
}
hero_mc.onEnterFrame = move;
I have a movieclip named hero and his eyes should look in the direction hes walking to. for that i created him with frame 1 beeing him looking at the player, frame 5 up, frame 10 left, frame 15 down and frame 20 right.
in a diffrent flash a function similar to this worked... but this time it does not.
anyone know why?
Thanks in advance
bluemagica
06-25-2009, 08:42 AM
this.gotoAndStop();
Kajotex
06-25-2009, 04:21 PM
Ah alrite!
Ok another problem..this is getting ridiculus :D
When the hero enters the "stairs" (the black square) he gets moved to a different frame. all cool and dandy but... why is there a shine though eye in his eye?
hopefully anyone can decypher my gibberish.
I'm kind of surprised the use of this made a difference. I thought that this was implied.
You have holes in your eyes. You don't notice against a white background, but go along the edge and you'll see green holes.
Kajotex
06-25-2009, 05:12 PM
I'm kind of surprised the use of this made a difference. I thought that this was implied.
You have holes in your eyes. You don't notice against a white background, but go along the edge and you'll see green holes.
ahahahahahaha
thanks haha :D
Kajotex
06-25-2009, 09:25 PM
Ok... doubleposting is my business :)
got a bit further.. but now i have the problem that a hittest function does not seemto work. its about the hit testof sword and enemy. both created movieclips and i want to have enemyvanis as it is being hit with the sword...but the hit detection seems not to workon these two.
anyone know the solution?
Kajotex
06-28-2009, 03:34 PM
Anyone please?
Mazoonist
06-28-2009, 04:56 PM
Your hitTest is not working because you have it in there in the wrong place. I should say that it IS working, it's just that it only works once, when your file first starts running, and it evaluates to false. There is a big difference between code that executes only once and code that executes continually. If you want your hit testing to happen continually, you have to place the code in an onEnterFrame event. So just add it into one of your existing ones, like so:
this.onEnterFrame = function() {
if (hero_mc.hitTest(stairs_mc)) {
gotoAndStop(10);
}
if (ENEMY.hitTest(SWORD)) {
removeMovieClip(ENEMY);
}
};
Kajotex
06-28-2009, 06:53 PM
Ah ok. learning something new every day :D Thanks!
another question.. i feel stumped, there is no wait funcion in AS2.0?
Wanted to make the enemy mc's walk across a pattern.. but i cant even sem to let them walk on a line correctly. the machine just calculates so fast that it appears to teleport from start to end. but i want my little red blob to move slowly across the path i give him. searched and found a coulple of functions. but either they are doing nothing or they are nearly crashing my program.
just one example:
while (e<30) {
ENEMY._y = ENEMY._y-5;
e++;
ENEMY.stop();
codeInt = setInterval(delayFunc, 30000);
function delayFunc() {
ENEMY.play();
clearInterval(codeInt);
}
}
};
anyone able to come up with a solution that does not require a supercomputer to run? ;)
Mazoonist
06-28-2009, 07:16 PM
It's a pretty well known fact that you can't use loops to animate anything in Flash. You are correct, it's blazingly fast, the whole loop runs before flash can render the next frame of animation.
I'm kind of surprised you are not catching on to the real answer, though. To animate anything in Flash, just keep putting the actions in the enter frame functions.
For example, change your enter frame event to this version:
this.onEnterFrame = function() {
if (hero_mc.hitTest(stairs_mc)) {
gotoAndStop(10);
}
if (ENEMY.hitTest(SWORD)) {
removeMovieClip(ENEMY);
}
ENEMY._y += enemySpeed;
if(ENEMY._y < 0 || ENEMY._y > Stage.height) {
enemySpeed = enemySpeed * -1;
}
};
Also add this line somewhere in your base level code (that is, anywhere outside of any functions):
enemySpeed = 5;
This will make the enemy go up and down on the y axis. When he gets to either the bottom or top of the screen, his speed will be reversed by being multiplied by -1. This will make him bounce back from the borders of the screen. Now you can play around with this basic idea and probably get close to the motion path you want (maybe). Or even use some kind of random motion. To get really sophisticated motion paths, though, you'd probably have to use an array of screen locations or something.
Kajotex
06-28-2009, 07:30 PM
ah ok..... i just understood onEnterFrame ;)
it triggers once a Frame gets created which is all the time.
but what if i really just want to delay something. for example.. i want my herosprite to loose health when he touches an enemy which is really easy. but in mycurrent design he has no "invulnerability" in between taking damage so its just instant death if he touches an enemy and is not fast enough to react within 0.1 seconds to evadethe nextblow. how is it possible to delay this. i know how i could make him invulnerable..i just dont know how to contril timeintervalls in flash. also, replaing the hero may be a good start and i'll use it but it would be nice to have an alternative ;)
EDIT: on asecond note, how is it possible to let movieclips only clip in their given shape?
when i have a mc formedlike a circle it still clips like its a square for example.
Mazoonist
06-28-2009, 07:54 PM
Yes, onEnterFrame() is mis-named, I've always thought. It should say runsAtTheFrameRateEvenIfThereIsOnlyOneFrame(), but that might be a bit unwieldy.
If you really want to delay something, use setInterval. Okay, I will give you an example, and then you will have to apply this simple example to whatever it is that you REALLY want to do. But I will show you how to make an interval (pause) in your program.
Let's suppose that you want the enemy, when he is delivered a death-blow with the sword, to wait three seconds to die (disappear). Change the onEnterFrame to this version:
this.onEnterFrame = function() {
if (hero_mc.hitTest(stairs_mc)) {
gotoAndStop(10);
}
if (ENEMY.hitTest(SWORD)) {
setInterval(removeEnemy, 3000);
}
ENEMY._y += enemySpeed;
if(ENEMY._y < 0 || ENEMY._y > Stage.height) {
enemySpeed = enemySpeed * -1;
}
};
SetInterval takes two parameters (optionally three, but that's another story): the function to call after the interval runs, and the time to delay--in milliseconds. So 3000 would be three seconds.
Next, you have to create the removeEnemy function that you have given as the first parameter. So add this function somewhere else in your code:
function removeEnemy() {
removeMovieClip(ENEMY);
}
Notice that the command to remove the enemy movie clip was simply moved to the other function instead, the one that runs after a three second delay.
To answer your other question, the hitTest method always uses the bounding box of whatever movie clip it is. So for square or rectangular clips, it works fantastic. But for round or irregular ones, it's way less than ideal. There is another form of hitTest that considers the actual graphical shape of the object, but hit tests it against a point (an x, y location). A hit test that considers the actual graphics of BOTH objects is harder than you think, you'd probably have to write your own. I haven't done that, but I have been a few rounds with making collision detection work. Consider the hit testing in this: http://www.mazoons.com/flash/listen_maze_black.html
bluemagica
06-28-2009, 09:07 PM
A enterFrame event runs at the current framerate, that's something you can't change.... so if you put a hitTest inside the enterframe, then its obvious the hero will get a constant damage and hence, instant death! But what you can do here, is to control when the desired hitTest code can or cannot run, using a logical switch called Boolean!
Pseudo code(logical form of code) for Boolean:-
if switch is on
execute code
so no matter how many times your enterFrame is executed, if the "switch" is off, the code won't actually run!
Pseudo usage:-
if hero has taken damage,
make him not capable of being damaged for 3 seconds
Real code
var can_be_hit = true; //A boolean can be either true(on) or false(off)
var counter = 0; //you can use setInterval, but i like using this method
this.onEnterFrame = function()
{
if(can_be_hit==true) //if switch is on
{
this.hp-=5
can_be_hit = false; //next time this portion won't be executed
}
if(can_be_hit==false && counter<90) //consider frame rate 30, for 3 sec = 90
{
counter+=1;
}
else
{
counter=0;
can_be_hit = true; //reset after 3 sec
}
}
Yeh this thing can be done in a simpler way with setInterval, but i wanted to show you the use of Boolean, which will be really handy to you in future! For example, making the user press the key repeatedly to hit, instead of just holding it down, requires use of boolean!
For shape based collision detection, you can use bitmapData based hitTests, but that's a bit advanced for you at this stage.... so stick to normal hitTest, or fake it with samll, invisible, mcs! (Put a small mc with alpha set to 0, at the place where you want the actual hit testing to occur, like tip of the sword of enemy, and check for collision against that, like this.hitTest(Enemy.swordTip_mc))
Kajotex
06-28-2009, 10:16 PM
Alrite, thanks Mazoonist and bluemagica :)
I think ill be able to pull this off now smoothly :)
Mazoonist
06-29-2009, 04:21 AM
I second what bluemagica said about Boolean variables. They're very handy. Also about hitTesting in an onEnterFrame event. It keeps on happening unless you turn it off somehow or prevent it with the aforementioned Boolean variable trick. I knew all that...
But I just wanted to just give you the immediate help you needed without overloading you with too much. I knew not everything I gave you was 100% kosher, but it should give you a start. The other stuff will come in time, and you'll acquire your own techniques, should you decide to keep pursuing it.
I would get away from Actionscript 1.0 as soon as possible, though, before you get too many bad habits you'll have to un-learn.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.