View Full Version : Punch Problem
JTurner
July 8th, 2009, 05:54 AM
hy
i am trying to make a character punch and kick, i have the code to work but when i enter the movement code in and then try to press 1 and 2, nothing happens, the actions are cancelling each other out or they are having an effect on each other but i still cant find a way to fix it, here is my code
walkspeed = 5;
walk = true;
up = 38;
down = 40;
backward = 37;
forward = 39;
punchcombo = 49; // keyboard 1 for punch combo
kick= 50; // keyboard 2 for kick
jumpkey = 51;// keyboard 3 for jump
function movement () {
moving = false;
if(Key.isDown(Key.RIGHT)){
hero._x += walkspeed;
hero._xscale = 100;
moving = true;
}
if(Key.isDown(Key.LEFT)){
hero._x -= walkspeed;
hero._xscale = -100;
moving = true;
}
if(Key.isDown(Key.UP)){
hero._y -= walkspeed;
moving = true;
}
if(Key.isDown(Key.DOWN)){
hero._y += walkspeed;
moving = true;
}
if(!moving && !attacks) {
hero.gotoAndStop("stance")
}
function attacks() {
if(hero.attack)
return;
if (Key.isDown(punchcombo)) {
hero.gotoAndStop("punch"); // if there's no combo, then the normal attack is played
hero.attack = true;
}
if (Key.isDown(kick)) {
hero.gotoAndStop("kick");
hero.attack = true;
}
}
};
//------------------------
onEnterFrame = function () {
//trace(Key.getCode());
movement();
attacks();
};and also heres my fla and swf
http://www.swfupload.com/view/104612.htm - swf
http://www.sendspace.com/file/ib7nns- fla
thanks in advance =)
wkt
July 8th, 2009, 09:08 AM
Wait a second... you put the attack function inside the movement function. When you call the attacks function in the onEnterFrame = function() {}, Flash can't find such a function to refer to.
I think putting the attacks function outside the movement function can solve the problem. Try it out.
JTurner
July 8th, 2009, 12:48 PM
nop it didnt work, but thanks for pointing that out....ive changed it now, still not working
rrh
July 8th, 2009, 01:09 PM
You use
if(!moving && !attacks) {
attacks is the name of your function, and you might have meant attack, which is what you are using elsewhere as a boolean.
wkt
July 8th, 2009, 01:12 PM
walkspeed = 5;
walk = true;
var moving:Boolean
var attack:Boolean
up = 38;
down = 40;
backward = 37;
forward = 39;
punchcombo = 49; // keyboard 1 for punch combo
kick= 50; // keyboard 2 for kick
jumpkey = 51;// keyboard 3 for jump
function movement () {
moving = false;
if(Key.isDown(Key.RIGHT)){
hero._x += walkspeed;
hero._xscale = 100;
moving = true;
}
if(Key.isDown(Key.LEFT)){
hero._x -= walkspeed;
hero._xscale = -100;
moving = true;
}
if(Key.isDown(Key.UP)){
hero._y -= walkspeed;
moving = true;
}
if(Key.isDown(Key.DOWN)){
hero._y += walkspeed;
moving = true;
}
if(!moving && !attack) {
hero.gotoAndStop("stance")
}
}
function attacks() {
if (!moving) {
if (Key.isDown(punchcombo)) {
hero.gotoAndStop("punch"); // if there's no combo, then the normal attack is played
attack = true;
} else if (Key.isDown(kick)) {
hero.gotoAndStop("kick");
attack = true;
} else {
attack = false
}
}
}
//------------------------
onEnterFrame = function () {
//trace(Key.getCode());
movement();
attacks();
};
I can't download your file. But I simulated your case by drawing a square as the hero. I've made some changes and it does work. Your code is just a bit messy. Write your logic out slowly and clearly and you can do it. Work hard!
All the best!
WKT, KEC Project
JTurner
July 8th, 2009, 04:34 PM
thank you soooo much wkt, your code worked like a charm...but (why does there always have to be a but) when i try putting in a line of code for the walking animation e.g.
if(Key.isDown(Key.RIGHT)){
hero.gotoAndStop("stand_forward");
hero._x += walkspeed;
hero._xscale = 100;
moving = true;
}
thats applied to left, up and down key btw, and also i put gotoAndPlay(1); at the end of the walking keyframe in the movieclip of the hero - to loop the animation...and now when i running the flash using ur code, he just keeps running until 1 or 2 is pressed...ive also add hero.attack = true/false instead of attack = true/false so that when 1 and 2 a tapped they dont cancel each other out and either a punch or kick animation is shown but not both
thanks again :D
wkt
July 8th, 2009, 09:33 PM
What about changing that line you put at the end of the walking keyframe to gotoAndStop(1). Also, when the walking keys are not pressed, send the current frame back to 1 so that hero stops walking.
You need to remember one thing: programming is like teaching/training a bloody stupid dog to do some actions you want it to do. Because you are a human being, your mind works too quickly. Very often, you assume (accidentally) that the computer will do what you want.
The computer will do what you write. A human can interpret missing logic but a computer can't. So your code must take all possible conditions into consideration so as to make the computer do what you want to do.
If a piece of code doesn't work at all, that means there are contradictions. Codes cancel out each other. If an animation plays continuously without stopping when you want it to stop, that means you did not specify when and where to stop.
flyingmonkey456
July 8th, 2009, 09:42 PM
i couldn't quite understand what you were saying, but i think i got it. tell me if i'm wrong, you're having a problem getting an action to finish before another action is able to start? that's simple, have a boolean for each action and when a key is pressed check each boolean to see if another action is already occurring. make sure you check each action's own boolean as well, or you'll be able to do super fast punching by spamming the punch key.
TOdorus
July 8th, 2009, 10:22 PM
What about changing that line you put at the end of the walking keyframe to gotoAndStop(1). Also, when the walking keys are not pressed, send the current frame back to 1 so that hero stops walking.
I agree, there are several ways to do this. One would be to check if a character is idle (not doing anything special) or not. A character would be idle unless it would do something, would be the easiest definition to check
var idle:Boolean = true
if(keypress.someKey){
doSomeStuff()
idle = false
}
have a boolean for each action and when a key is pressed check each boolean to see if another action is already occurring. make sure you check each action's own boolean as well, or you'll be able to do super fast punching by spamming the punch key.
This would work ok when there's only two actions, but if there are several this brings up you checking a lot of booleans. Why not use a integer instead with a few constants?
public const IDLE:int = 0
public const PUNCHLIGHT:int = 1
public const PUNCHHEAVY:int = 2
public const KICKLIGHT:int = 3
public const KICKHEAVY:int = 4
//
//
//
switch(currentAction){
case IDLE:
gotoAndStop("Idle")
break;
}
This also brings up a more scalable way to handle the player bieng idle (or not).
var currentAction:int = IDLE
if(prevAction == IDLE){
if(keypress.lightPunchKey){
currentAction = LIGHTPUNCH
}
if(keypress.heavyPunchKey){
currentAction = HEAVYPUNCH
}
if(keypress.lightKickKey){
currentAction = LIGHTKICK
}
if(keypress.heavyKickKey){
currentAction = HEAVYKICK
}
//
switch(currentAction){
case LIGHTPUNCH:
break;
case HEAVYPUNCH:
break;
case LIGHTKICK:
break;
case HEAVYKICK:
break;
}
}
//
prevAction = currentAction
JTurner
July 9th, 2009, 06:27 AM
@ wkt - i have tried replacing gotoAndPlay with gotoAndStop but when the animation frame finshes and a arrow key is still pressed, the character hovers instead of repeating the animation frame
@flyingmonkey - that was an issue but i have corrected it now thank you
@TOdorus - thanks, i am currently trying your way and i will tell you if i am successful
Before i repeat un-neccessary work, what is the best possilbe way of making a fighting game? e.g. the way i am currently doing it or the way Todorus suggested i.e. using constants - as later on i want the character jump, execute punch chains (like punch, heavy punch, kick), run, and move combos (about 5 combos)...also as the character itself will be exported into another flash file by attachMovieClip would it be better to use constants?
thanks again guys :D
SparK_BR
July 9th, 2009, 06:55 AM
try putting this code in the last frame ;)
if(!moving) gotoAndStop(1);
-----------------
if that didn't work try this code in the first frame:
if(!moving && !attack) stop();
and this in the last frame:
gotoAndPlay(1);
JTurner
July 9th, 2009, 10:35 AM
Sparks....its not worked, ive even tried to replace gotoAndStop with gotoAndPlay but it just made it worser
SparK_BR
July 9th, 2009, 11:12 AM
did you use the attack and moving vars with the right scope?
like adding this. or the path to the player vars? i think they are kinda undefined if they can't be found.
TOdorus
July 9th, 2009, 12:10 PM
Before i repeat un-neccessary work, what is the best possilbe way of making a fighting game? e.g. the way i am currently doing it or the way Todorus suggested i.e. using constants - as later on i want the character jump, execute punch chains (like punch, heavy punch, kick), run, and move combos (about 5 combos)...also as the character itself will be exported into another flash file by attachMovieClip would it be better to use constants?
Could you be more specific?
How would you like the combo's to work? That when a player hits buttons while another move is executed it is stored in a list? That when a player hits a button near the end or just after a move ends it flows into a new move, with the new move based on the earlier attacks in the sequence? For both options I'd use an array btw. It also opens up rule-based AI to predict which move the player will use next (it has punched it will probably do a punch again as the player uses a lot of punch-punch combo's. No he kicks, then it's probable that he kicks again because he hasn't been likely to punch after a punch-kick combo). The constant gives the AI a feed what reaction it should throw.
About the importing by attachMovie: I have no idea what you're talking about. You mean that you'll dynamicly create movieClips (create them while the program is running)? Then we're just using different termenologies. I don't see how that would be a problem anyway, if you're coding on the first frame. If you're coding on movieclips themselves then it becomes a bit harder on us to offer help, as we wouldn't know how you've structured your code. Some on the first frame, some inside a movieclip some on a movieclip. Like Spark said: we'll get scope issues.. In case you haven't, it's quite easy to adapt to coding on the first frame when you create movieclips dynamicly.
Charleh
July 9th, 2009, 12:43 PM
You can use the code I did in this thread if you like
http://www.kirupa.com/forum/showthread.php?t=272699&highlight=double+dragon
It's a simple double dragon engine (well the beginnings of one!)
If you do decide to use it, let me know if you need any help figuring things out.
(It's AS2.0 btw, so may need porting to 3)
JTurner
July 9th, 2009, 07:30 PM
@ Charleh - im gonna have a go at ur engine...thanks for showing it me
@ Todurs - ill try to explain what im trying to achieve....i want to make a game similar to streets of rage (as you can probabily tell from the graphics ive used) however the game will be like a combination of street fighter and streets of rage...
the punch combo will be like, if there is a successful hit e.g. punch then the character will perform another punch animation and if that hit is successful aswell then it will perform a kick finally knocking the enenmy on the floor.
the move combos will be similar to the ones in street fighter i.e. press up and jump, attack will do a fireball...i hope to have about 4-5 different combinations, so i could possilby use an array
what i mean by the AttachMovieClip thing, is that the character will be imported to another swf e.g. a background and along with importing the character.swf the inventory will be imported aswell.
how would i structure my game then? would it be better using scopes of varaibles or continue the way i am doing...hope this helps..if not i can elaborate more if needed
bluemagica
July 9th, 2009, 07:56 PM
err, a fighting game is one of those genres, which can as simple or as complex you want it to be! I didn't actually go through your code, as I assumed it should have gone through some major changes by now. Anyway, to make the fighting game, you first need to make the logic clear to yourself.....code your thing is pseudocode first, like
1) <movement>
<if not attacking>
<check if in air>
<left,right movement>
<jump>
<else>
<gravity>
<if not doing anything above, stand>
<end movement>
<attack>
<check combo1 conditions>
<check punch>
<if previous done, do next punch>
<if previous done, do kick>
<else check combo2 conditions>
.........
......
<end attack>
Once you have something like above, break each step into sub-steps, and you will be able to code your game without a logic hitch!
Well, that may sound boring, but it will help you actually speed up the development!
And whats up with the character/inventory importing part? Are you using external resources? And sorry, to ask again, but the last few replies got me lost, which portion are you exactly stuck at? cause the only thing wrong is your logic implementation of the movement and attack functions!
TOdorus
July 9th, 2009, 08:56 PM
how would i structure my game then? would it be better using scopes of varaibles or continue the way i am doing...hope this helps..if not i can elaborate more if needed
Before I say anything else: Actually both will produce the same results. It's a matter of how you "write down" your data sort of speak. I prefer the constants method because it's a bit more flexible/scalable, but I reckon others might disagree.
the punch combo will be like, if there is a successful hit e.g. punch then the character will perform another punch animation and if that hit is successful aswell then it will perform a kick finally knocking the enenmy on the floor.
I tried your links, but I couldn't find any files to download? Anyways,
I'm yet to think of how to succesfully do the detection part. You can't really expect players to press the next button at the exact time that the attack connects. You'll have to make your attack hang dead in the air for a bit. Say 5 frames? The character punches: the arm extends and a fist connects with the enemy. Now the arm stays extended for a while, giving the player time to press another button to combo. If the attack doesn't connect there is no need to let it hang dead in the air, and the arm can retract immediatly after finishing the attempted attack. Also, you don't want the player to feel punished for succesfully hitting an enemy by bieng left open to attack, so in that moment the player wants to block, jump or whatever he should be able to immediatly do that as if the character was idle.
The advantage of constants really kicks in here as you can use these to check for what the next attack would be in the combination, instead of having to create a seperate system for this (which probably would need constants anyway).
Then again: Charlehs engine might do this in a more sophisticated way and I'm talking total rubbish.
what i mean by the AttachMovieClip thing, is that the character will be imported to another swf e.g. a background and along with importing the character.swf the inventory will be imported aswell.
That sounds like a dynamicly created movieClip to me, as I don't know a way to have a movieClip at authortime when you still need to import it into the library. In that case there isn't much of a problem. If you have a class structure in place there shouldn't be any problem, as the movieClip can be assigned to a custom class. In the case that you can't directly assign it to a class (like you would do in the export properties dialog) by importing it, you can always create a custom class and assign the movieclip to it.
class FighterClass {
private var Graphic:movieClip
public var currentAction
public function FighterClass (GRAPHIC:movieClip){
Graphic = GRAPHIC
}
public function updateGraphic(){
switch(currentAction){
case LIGHTPUNCH:
Graphic.gotoAndPlay(someFrame)
break;
}
}
}
EDIT: btw, if it ever comes anything close to this:
http://www.youtube.com/watch?v=WcmBbauIWxs&feature=PlayList&p=FAA91E83190DB8B2&index=3
god I loved that game.
JTurner
July 10th, 2009, 05:37 AM
@ bluemagica - i am going to writing the pseudo code as we speak and ill post it soon as ive finshed to see if right or not
To all - sorry if i have confused you lot, but when i meant AttachMovieClip, i was meant to say MovieClipLoader, now to clear things up....i want to load external swf's such as characters, backgrounds, etc using the MovieClipLoader not AttachMovieClip
sorry again for confusing you guys....thanks
Charleh
July 10th, 2009, 05:55 AM
Todorus, most of that is covered in the code I posted
What you do with my engine is create a series of frames in your character movieclip/sprite which contain all the attack animations.
You then create a set of animation objects which you assign to the player or enemy characters, each animation object simply contains an array of frame numbers and delay times for those frames. Animations can be for anything, walking, jumping, dying, punching etc
Each frame in an animation can have attack information tied to it which shows where and how hard an attack will take place within that frame. The attack information also contains a 'recovery' time, which is how long it takes the character to be able to move/block/jump again after the attack, and also a 'follow-up' time, which is how long the user has to enter another attack command for a combo. (Usually it's best to make these the same, or to make the recovery time a bit more than the follow-up time)
Attacks also have a follow up key which can lead to either a punch or kick follow up attack, some of which will launch the player into the air to allow them to perform some air juggles/moves.
It's not perfect, it was just a thrown together concept, but it works well. I'm sure I can tidy it up and make it easier to use, but so far in the engine you have walking, jumping, combos and flying kicks etc, but no bad guys :)
JTurner
July 12th, 2009, 06:00 PM
Charleh, before i dabble even futhur...it it possible to do perfrom hittests or sophisted collision detections like bitmapdata using classes and will need to use external external AS files or can i put in the fla? and also is it possible to make it do all sorts of things like running, running punchs, etc using classes?
thanks
SparK_BR
July 13th, 2009, 02:50 AM
Charleh, before i dabble even futhur...it it possible to do perfrom hittests or sophisted collision detections like bitmapdata using classes and will need to use external external AS files or can i put in the fla? and also is it possible to make it do all sorts of things like running, running punchs, etc using classes?
thanks
i'm not charleh but... it is possible, just try to abstract what you need with real world situations, and before pseudo coding, you must know what to pseudo code, so making a class diagram and/or a "use case diagram" using you classes as systems/users and the sub-classes of the current module you are developing will clarify things first
it will give you some extra work but it's recommended for a big project like yours
Charleh
July 13th, 2009, 05:41 AM
It's possible to perform anything you like using classes - like SparK said, it's just about organising your brain and making sure that translates to code.
You would probably do most of your code in the external .AS files. That's part of the point of using classes, everything is separate and broken up into discrete chunks. Putting stuff in the .fla only gets confusing!
The hit tests in these games are rarely bitmap based though, most of them use primitive circle/ellipse or hitbox tests. You can do running/running punches etc, you just need to implement them :)
Point in box tests are simple, and box overlap tests are also very simple. These collisions can also be done 'brute force' in a game like Double Dragon without having to worry about spatial partitioning or anything like that, because most of the time you only have a handful of bad guys on the screen, so it's really quite simple.
All I do when coding is tackle one problem at a time whilst thinking about any impact that might have on the rest of the code and trying to make sure things are re-usable.
In the engine demo, I think the animation class could be tidied up a bit by adding a new class to handle the frames instead of using 2 arrays in the animation object, and the combos could be tidied up a bit. I was also thinking about moving the attack data to an external file so that the combos could be edited outside of the game, and reloaded with a press of the key, so you could tweak the attack timings and animations outside of the game whilst it was running, and then 'refresh' them to test them out, without having to recompile.
If I do get some free time I might try implementing some of that stuff, but I have wife/kids to sort most of the time :)
JTurner
July 28th, 2009, 12:39 PM
hy, sorry i took so long to reply, its been a busy week....but i have written some pseudo code but i am not sure to if its correct, so here goes
<movement>
<if not attacking, jumping, running or doing special>
<if right key down>
<movement_right();>
<else if left key down >
<movement_left(); >
<end if>
<if up key down>
<movement_up();>
<else if down key down>
<movement_down();>
<end if>
<end if>
<if jump key down and !jumping>
<jump();>
<else>
<player.stance>
<end if>
<if not attacking, jumping, running or doing special>
<player.stance>
<end if>
<end movement>
<attack>
<if not attacking, jumping, running or doing special>
<check combo1 conditions>
<check punch>
<if previous done, do next punch>
<if previous done, do kick>
<else check combo2 conditions>
.........
......
<end if>
<if jumping and punch key down>
<player.jumppunch>
<end if>
<If jumping and kick key down>
<player.jumpkick>
<end if>
<if running and punch or kick key down>
<player.runningpunch>
<end if>
<end attack>
im not too sure if i have written it correctly...any pointers will be handy
thanks
SparK_BR
July 29th, 2009, 01:34 PM
hmm... did you notice that there are a lot of repeated code?
well... those are logic problems, i will see if i can help:
<movement>
<if not attacking, jumping, running or doing special>
<stand>
<end if>
<if right key down>
<walk right>
<else if left key down >
<walk left>
<end if>
<if up key down>
<walk up>
<else if down key down>
<walk down>
<end if>
<if jump key down and not jumping>
<jump>
<jumping>
<end if>
<end movement>
<attack>
<if not jumping, running or doing special>
<get list of the last movements>
//combo 1
<if punch done>
<if punch done>
<if kick done>
<special kick or punch>
//combo 2
<if kick done>
.........
......
<end if>
<if jumping and punch key down>
<jumppunch>
<attacking>
<end if>
<If jumping and kick key down>
<jumpkick>
<attacking>
<end if>
<if running and punch key down>
<runningpunch>
<end if>
<end attack>
JTurner
July 29th, 2009, 06:16 PM
thanks Sparks, ill give it a go...but as i am new to AS3 (i am more familar with AS2) but is this all possible in AS3 because i there is alot of changes in AS3 like theres no keydown function, or is it better of doing this in AS2?
SparK_BR
July 29th, 2009, 10:12 PM
Key.isDown exists it's just not recommended...
JTurner
July 30th, 2009, 07:56 AM
would it be better creating it in AS2 and using OOP...keeping in mind that i will be handing the project to another person to expand on it? or is it possible that i create in in AS2 and when i hand the project over, they can build on top of it using AS3...or will that cause problems in the long run
Charleh
July 30th, 2009, 02:24 PM
You will find that you are forced to program better in AS3. AS2 lets you get away with murder, and theres more support for advanced features in AS3.
Also AS3 is a bit faster. AS2 still supports bitmapdata though, so you can use that for rendering to get lightning quick rendering with plenty of objects/special effects on screen!
I'd try with the AS3 first
SparK_BR
July 30th, 2009, 04:07 PM
/agree
if you don't force you to think and learn the AS3 language now you will never be able to overcome your future problems with it.
hold on, and try to learn it and take the maximum advantage of it.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.