PDA

View Full Version : Foe Does An Random Attack?


Sythius
04-22-2009, 01:34 PM
I need more help sorry! ^^;
Is there a way to make it so the enemy monster will use different attack moves?...
I ideally need some coding that only has to be on one frame.

And to trigger the "attack" it needs to GoToAndPlay a named frame.
... Thanks for any help =)

I have tried myself..
I have tried Random and Math.Random. And I had 2 If commands that had GoToAndPlay at the end of.
But I had issues with getting the If commands to work, as well as the script not doing another Random thing to the Dynamic Text field.
I've looked at the roll dice-like command, but that just confuzed and I couldn't implement it myself o.o

rrh
04-22-2009, 02:22 PM
If you have a whole list of attacks, it might be neater to use a switch/case statement.

http://www.tech-recipes.com/rx/2369/as3_switch_case_statement_syntax/

And remember that Math.random() doesn't give you integers unless you Math.floor() or Math.round() the results.

Sythius
04-22-2009, 03:52 PM
Hm..
... From the looks of it that only seems to sort out the If problem..
Which wasn't really he problem..
My problem was that it's meant to go to this frame. With the coding.
A random number is done.
And whatever that number is, corresponds with an attack and GoToAndPlay's at that frame.
But my problem is... It will only do the random number once, then it just like... get's stuck.. never changes. .. Even though it says Random..

bluemagica
04-22-2009, 05:01 PM
1) make each attack a separate mc
2) On the last frame of each put "this._parent.can_attack = true;"
3) Make a new mc, say "monster", and on each frame of it put one of the attack mcs, and name the frames!
4)make a new layer within the monster mc, and on the first frame put this:-

can_attack = true;
attackNum = Math.ceil(Math.random()*5);
this.onEnterFrame = function()
{
if(can_attack==true)
{
can_attack=false;
switch(attackNum)
{
case 1: gotoAndStop("UselessAttack1");
break;
case 2: gotoAndStop("UselessAttack2");
break;
case 3: gotoAndStop("annoyingAttack1");
break;
case 4: gotoAndStop("UselessAttack3");
break;
case 5: gotoAndStop("flee_away_attack");
break;
}
}
}

well, i haven't tested this, so there can be few problems and errors, but this is the general way for as2!

Sythius
04-22-2009, 05:49 PM
Hm...
I don't think that's compatible with the rest of my game o.o
As the res tis based on actual frames, etc.

bluemagica
04-23-2009, 04:43 AM
err...then do try to elaborately explain your game structure, and why isn't it exactly compatible?

Sythius
04-23-2009, 07:30 AM
Oh right. Soz. lol.
Well I have 3 Layers in my game named in order;
Top; health
Middle; DMG count
Bottom; battle
Last; bckgrnd (Has nothing to do with any of the game; just backgrounds XD )
Top holds the dynamic text fields that attacks deal damage to.
DMG Count shows the damage done, getting it's own layer so I can animate it to bounce, and choose exactly how long it lasts.
Last is battle. This is where most of it happens. this is where my frame-by-frame attacks play out. The attacks aren't in any movieclips. Partially due to to the easiness of leaving frame by frame on the layer, etc.
While what you got there looks like it would work by itself.. But form what I've found at least. You can't GoToAndStop/Play to any frame outside of that movie clip. which is something I would need to do a lot.
After each of the foe's attack I have;
if (_root.good<=0) {
gotoAndStop("lose");
}
if (_root.good>=0) {
gotoAndStop("home");
}

Home is the attack menu. and lose is self explanatory.

bluemagica
04-23-2009, 09:11 AM
You can't GoToAndStop/Play to any frame outside of that movie clip
Who said you can't do that? well, sure it isn't the OOP way, and real programmers will chew my head off if i suggest doing it, but it can be done easily! You just have to reference the correct timeline.

See, in my code, i asked to use "_parent", that referes to the movieclip outside the one you are calling, so using that i can easily take control of the parent timeline! Actually in as2, there is almost no such thing as "scope" when you compare it to as3! You can access almost anything from anywhere!



I suggest you read through this tutorial, it will hopefully clear up your doubts:-
http://www.gotoandplay.it/_articles/2006/06/beatemup.php

Sythius
04-23-2009, 01:33 PM
Haha. Well...
that link confuzed the hell outta me..
Regardless.. I attempted to use what you said....
I stumbled onto 2 problems.
1st; no matter how many times I ran it. It ALWAYS used attack 3.
2nd; it's looping the attack... It won't take the timeline to a different frame.. I tried to ways.. (I am a newb of any code btw =D );
First tried.. gotoAndStop(_parent.home) .. didn't work. ..so looked on the net.. and tried..
_parent.gotoAndStop("home");
that didn't work either. I then tried..
_parent.gotoAndStop(2);
Which is the frame number. All didn't work.

Update;
After all these changes etc..
I have found that that it cycles through the attacks and then stops and executes it...
(Instead of having 5 different attacks, I just past'd "attack 1" etc on each...)
and as soon as my player's frames has finished, it moves onto the foe's.
And now I get like a... Countdown in reverse.. you can literally see it go "attack1" "Attack2" etc... until it seems to randomly stop on one, and does the rest of that one's animation.
(only does first frame of the rest)
... And it really is random. As the time it takes tog et to attack 5, takes longer than ti does to say Attack 1.. it doesn't skip or jump to that attack. It seems to.. count up to it... Is it meant to do this?

bluemagica
04-23-2009, 04:37 PM
show your fla!

Sythius
04-23-2009, 06:04 PM
Here ye go.

rrh
04-24-2009, 02:55 AM
Yeah, that's kind of convoluted, but...

One thing to note is that atk5 is a child of monster, which in turn is a child of the main timeline.

_parent.gotoAndStop(16); //so if this is in atk5,
//then it will try to make monster go to frame 16
//but monster isn't that many frames long

If you want the main timeline, you need to use _parent._parent or _root

bluemagica
04-24-2009, 03:51 AM
:eek: that's quite scary, lol, it reminded me of dragonball!

Anyway, your parent-child logic is messed up! Google around a bit and try to clear it before proceeding! and also, please put the actionscript on its own layer...

Sythius
04-24-2009, 07:26 AM
Ahhh right.. Thank you rrh! :D
I understand it now =)
Plus it worked! :D