Play free games online!


Go Back   The Game Homepage Forums > Development > Flash Development

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 07-02-2008, 03:08 AM
Bigmutha's Avatar
Moderator
Power Drill
Better than 70.21% Better than 70.21% Better than 70.21%
 

Join Date: Aug 2007
Location: Chico, California
Posts: 1,757
Bigmutha is a jewel in the roughBigmutha is a jewel in the rough
Send a message via AIM to Bigmutha Send a message via MSN to Bigmutha
Default Keypress Animations

I want to make a movieclip called "runningleft" play when the the LEFT key is held down; vice versa. I have a code that does that well, except if you change directions without releasing both buttons he does a moon walk. It's something really obvious that I'm missing.

Help?


So far:
Code:
onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
        if(idle == true){
            gotoAndPlay("runningleft");
            idle = false;
            }
    } else {
        if (Key.isDown(Key.RIGHT)) {
            if(idle == true){
                idle = false;
                gotoAndPlay("runningright"); 
                }
        } else { 
        idle = true; gotoAndPlay("standing");
        }
    }
}
__________________

Last edited by Bigmutha; 07-02-2008 at 03:09 AM. Reason: typo
Reply With Quote
  #2  
Old 07-02-2008, 03:50 AM
gong29's Avatar
Gamer
Power Drill
Better than 76.05% Better than 76.05% Better than 76.05%
 

Join Date: Aug 2007
Location: Vinson Massif, Antarctica
Posts: 1,469
gong29 is a jewel in the roughgong29 is a jewel in the roughgong29 is a jewel in the rough
Send a message via AIM to gong29
Default

This Is For The Game We're Making! so it is important

just providing some motivation! (and a cookie to bribe (: we also need a cookie smily)
__________________

(thanks nate)
Reply With Quote
  #3  
Old 07-02-2008, 06:38 AM
noobjam's Avatar
Moderator / Flash Developer
Recycle Bin
Better than 47.39% Better than 47.39% Better than 47.39%
 

Join Date: Jul 2007
Posts: 433
noobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura about
Default

Original
Code:
onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
        if(idle == true){
            gotoAndPlay("runningleft");
            idle = false;
            }
    } else {
        if (Key.isDown(Key.RIGHT)) {
            if(idle == true){
                idle = false;
                gotoAndPlay("runningright"); 
                }
        } else { 
        idle = true; gotoAndPlay("standing");
        }
    }
}
Altered
Code:
onClipEvent (load) {
    idle = true;
}
onClipEvent (enterFrame) {
    Key.isDown(37) && !Key.isDown(39) ? (gotoAndPlay("runningleft"), idle=false) : Key.isDown(39) && !Key.isDown(37) ? (gotoAndPlay("runningright"), idle=false) : (idle=true, gotoAndPlay("standing"));
}
That's what I gathered you were attempting. Example.
__________________



Last edited by noobjam; 07-02-2008 at 06:40 AM.
Reply With Quote
  #4  
Old 07-02-2008, 06:40 AM
Bigmutha's Avatar
Moderator
Power Drill
Better than 70.21% Better than 70.21% Better than 70.21%
 

Join Date: Aug 2007
Location: Chico, California
Posts: 1,757
Bigmutha is a jewel in the roughBigmutha is a jewel in the rough
Send a message via AIM to Bigmutha Send a message via MSN to Bigmutha
Default

Quote:
Originally Posted by noobjam View Post
Original
Code:
onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
        if(idle == true){
            gotoAndPlay("runningleft");
            idle = false;
            }
    } else {
        if (Key.isDown(Key.RIGHT)) {
            if(idle == true){
                idle = false;
                gotoAndPlay("runningright"); 
                }
        } else { 
        idle = true; gotoAndPlay("standing");
        }
    }
}
Altered
Code:
onClipEvent (load) {
    idle = true;
}
onClipEvent (enterFrame) {
    Key.isDown(37) ? (gotoAndPlay("runningleft"), idle=false) : Key.isDown(39) ? (gotoAndPlay("runningright"), idle=false) : (idle=true, gotoAndPlay("standing"));
}
That's what I gathered you were attempting. Example.
Ya that's perfect. What was I doing wrong?

You have a very unique way of scripting also.

I've never used the "?" and ":" functions. Could you explain what they do?
__________________

Last edited by Bigmutha; 07-02-2008 at 06:43 AM.
Reply With Quote
  #5  
Old 07-02-2008, 06:47 AM
noobjam's Avatar
Moderator / Flash Developer
Recycle Bin
Better than 47.39% Better than 47.39% Better than 47.39%
 

Join Date: Jul 2007
Posts: 433
noobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura about
Default

Your mistake was you were letting people press both at the same time. I added a check that would only let them move right if the left key wasn't down and vice versa. If they were both down or both not, it would be idle.

Code:
condition ? statement : statement;


It's just an if statement that needs an else. Very short way of doing;

Code:
If(condition){ 
    statement 
} else {
   statement 
};
__________________


Reply With Quote
  #6  
Old 07-02-2008, 06:52 AM
Bigmutha's Avatar
Moderator
Power Drill
Better than 70.21% Better than 70.21% Better than 70.21%
 

Join Date: Aug 2007
Location: Chico, California
Posts: 1,757
Bigmutha is a jewel in the roughBigmutha is a jewel in the rough
Send a message via AIM to Bigmutha Send a message via MSN to Bigmutha
Default

Hmmm, noobjam? When I try it on my game, it doesn't play the running animation, it starts to play, but the next frame it starts all over. So he just shakes. It worked on yours because it wasn't a full animation, just a pic.

I'll check this in the morning noobjam, I have to go to bed, and my mom remembered to shut down her laptop tonight.
__________________

Last edited by Bigmutha; 07-02-2008 at 07:00 AM.
Reply With Quote
  #7  
Old 07-02-2008, 07:05 AM
noobjam's Avatar
Moderator / Flash Developer
Recycle Bin
Better than 47.39% Better than 47.39% Better than 47.39%
 

Join Date: Jul 2007
Posts: 433
noobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura about
Default

Code:
onClipEvent (enterFrame) {
    idle == true ? Key.isDown(37) && !Key.isDown(39) ? (gotoAndPlay("runningleft"), idle=false) : Key.isDown(39) && !Key.isDown(37) ? (gotoAndPlay("runningright"), idle=false) : null : null;
    (!Key.isDown(37) && !Key.isDown(39)) || (Key.isDown(37) && Key.isDown(39)) ? (idle=true, gotoAndPlay("standing")) : null;
}
Example.
__________________


Reply With Quote
  #8  
Old 07-02-2008, 08:22 AM
Gaz's Avatar
Gaz Gaz is offline
Administrator
Power Drill
Better than 63.31% Better than 63.31% Better than 63.31%
 

Join Date: Jun 2007
Location: Ipswich, UK
Posts: 1,022
Gaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to behold
Default

Looks like the problem is that if you never release an arrow button idle never becomes true so it won't get as far as the gotoAndPlay commands.

Here's my solution, it works similar to noobjams but is probably a bit easier to follow:

Code:
onClipEvent (load) {
 idle = true;
}
 
onClipEvent (enterFrame) {
 
  if ((Key.isDown(Key.LEFT) && Key.isDown(Key.RIGHT)) || (!Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT))) {
    // If both keys are up or down he is standing 
    idle = true; gotoAndPlay("standing");
 
  } else if (Key.isDown(Key.LEFT) && idle == true) {
    // Otherwise if the left key is down run left
    gotoAndPlay("runningleft");
    idle = false;
 
  } else if (Key.isDown(Key.RIGHT) && idle == true) {
    // Otherwise if the right key is down run right
    gotoAndPlay("runningright");
    idle = false;
  }
 
}
I got rid of the nested if statements too
Reply With Quote
  #9  
Old 07-02-2008, 08:31 AM
noobjam's Avatar
Moderator / Flash Developer
Recycle Bin
Better than 47.39% Better than 47.39% Better than 47.39%
 

Join Date: Jul 2007
Posts: 433
noobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura aboutnoobjam has a spectacular aura about
Default

Quote:
Originally Posted by Gaz View Post
stuff
Haha, Gaz comes along and shows the nice way of coding. I just crackdown and make it like 4 lines long.

I just picked up a few tricks along the way (You never really see it used or know it's used. but using binary and bitwise operators is a neat little trick)

Of course, I would never put script ON a MovieClip anymore. Timelines only.
__________________


Reply With Quote
  #10  
Old 07-02-2008, 08:44 AM
Gaz's Avatar
Gaz Gaz is offline
Administrator
Power Drill
Better than 63.31% Better than 63.31% Better than 63.31%
 

Join Date: Jun 2007
Location: Ipswich, UK
Posts: 1,022
Gaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to beholdGaz is a splendid one to behold
Default

Quote:
Originally Posted by noobjam View Post
Of course, I would never put script ON a MovieClip anymore. Timelines only.
I want to break the habit but it is convenient. I like everything neatly filed away in an on(load) and on(enterFrame) + use it all over the place. Damn you AS3!!!
Reply With Quote
Sponsored Links
Reply

Tags
as2, flash, walking

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 08:10 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2007, Crawlability, Inc.

Ad Management by RedTyger