PDA

View Full Version : as3 movieClip/keyboard control - im an as3 noob btw



frshhh
May 27th, 2009, 10:07 AM
OK, so im trying to make a seemingly simple as3 project where I instantiate a movieclip from the library, and then move it using keyboard controls. I got it working fine using ye olde as2 method (all the code on the timeline of the .fla), but i cant get it to work using the as3 .as file method (if that's the right description).

Here's what i have so far:

man.fla

import Hero;
var hero_mc:MovieClip = new Hero();
addChild (hero_mc);
hero_mc.Heromanctrl();
//hero_mc.checkKeys();
//hero_mc.keyUps();
hero_mc.x= 100;
hero_mc.y= 100;Hero.as

package {

import flash.display.*
import flash.events.*

public class Hero extends MovieClip {
var varRight:Number = 0;
var varLeft:Number = 0;
var varUp:Number = 0;
var varDown:Number = 0;
var xspeed:Number = 10;
var yspeed:Number = 7;
var animFrame:Number = 1;
var moveDir:Number = 0;

public function Heromanctrl() {
stage.addEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);
stage.addEventListener(Event.ENTER_FRAME, movement);
}

public function checkKeys(event:KeyboardEvent){
if (event.keyCode == 39) {
varRight = 1;
}
if (event.keyCode == 37) {
varLeft = 1;
}
if (event.keyCode == 40) {
varDown = 1;
}
if (event.keyCode == 38) {
varUp = 1;
}
}

public function keyUps(event:KeyboardEvent) {
if (event.keyCode == 39) {
event.keyCode = 0;
varRight=0;
}
if (event.keyCode == 38) {
event.keyCode = 0;
varUp=0;
}
if (event.keyCode == 37) {
event.keyCode = 0;
varLeft=0;
}
if (event.keyCode == 40) {
event.keyCode = 0;
varDown=0;
}
}

public function movement(Event){
moveDir = varUp + varDown + varLeft + varRight;
//--------------------------------------- RIGHT ----
if (varRight == 1) {
Hero.x += xspeed;
if (moveDir == 1){
parent.gotoAndStop(3);
animFrame = 3;
}
}
//--------------------------------------- UP ----
if (varUp == 1) {
Hero.y -= yspeed;
if (moveDir == 1){
parent.gotoAndStop(2);
animFrame = 2;
}
}
//--------------------------------------- LEFT ----
if (varLeft == 1) {
Hero.x -= xspeed;
if (moveDir == 1){
parent.gotoAndStop(4);
animFrame = 4;
}
}
//--------------------------------------- DOWN ----
if (varDown == 1) {
Hero.y += yspeed;
if (moveDir == 1){
parent.gotoAndStop(1);
animFrame = 1;
}
}
//--------------------------------------- STILL ----
if (varUp == 0){
if (varDown == 0){
if (varRight == 0){
if (varLeft == 0){
parent.gotoAndStop(animFrame + 4);
}
}
}
}
}
}


}As it is at the moment the mc loads on the stage, but is uncontrolable... if i uncomment the 2 other function calls in the .fla (//hero_mc.checkKeys();... etc) i just get an error:


ArgumentError: Error #1063: Argument count mismatch on Hero/checkKeys(). Expected 1, got 0.
at rpgman_fla::MainTimeline/frame1()I accept to some of you as3 masters, there could quite possibly be something really simple and totally nooby about my code, but like i say... i AM a total as3 noob. I have tried reading through tutorials and examples (such as the ones here on kirupa), but i can't get my head around it...

So please, get out your coding paddles, pull down my coding pants... and code-spank me silly... ahem.

mathew.er
May 27th, 2009, 10:40 AM
In AS3, if a function expects a required parameter, it has to be supplied. That's why you can't just call hero_mc.checkKeys ();

To make a parameter optional, you can specify a default value, here it would be

public function checkKeys(event:KeyboardEvent = null)But in this case, it's not what you'd like to do. The checkKeys method works with the event and you'd just get another error on the first line when you'd try to access it's keyCode property.

If your code doesn't work, try putting trace ( event.keyCode ) in the checkKeys method to see if it even has some of the expected values, othewise it seems like it should do at least something :)

frshhh
May 27th, 2009, 10:54 AM
In AS3, if a function expects a required parameter, it has to be supplied. That's why you can't just call hero_mc.checkKeys ();

To make a parameter optional, you can specify a default value, here it would be
ActionScript Code:

public function checkKeys(event:KeyboardEvent = null)


But in this case, it's not what you'd like to do. The checkKeys method works with the event and you'd just get another error on the first line when you'd try to access it's keyCode property.

If your code doesn't work, try putting trace ( event.keyCode ) in the checkKeys method to see if it even has some of the expected values, othewise it seems like it should do at least something :)
Hi and thanks for the reply mathew.er

I tried the trace and yes, it does output the keycodes... which is a start i guess, but still no movement (not that i expected there to be btw)

EDIT: WE HAVE MOVEMENT!

I changed the 'movement' function from

Hero.x += xspeed; to
parent.x += xspeed;

It now moves :sen:

...although, the gotoAndStop(#) don't work.... any ideas there?

mathew.er
May 27th, 2009, 11:25 AM
I was just about to post here :)

Yes, Hero.x won't work. Calling parent.x += xspeed doesn't move the "hero" either, it moves it's parent, change it to only x += xspeed;

frshhh
May 27th, 2009, 11:36 AM
I was just about to post here :)

Yes, Hero.x won't work. Calling parent.x += xspeed doesn't move the "hero" either, it moves it's parent, change it to only x += xspeed;
mathew, mathew, mathew.

i love you so much, i now have a damp gusset (bladder weakness, don't ask)...

i also fixed the gotoAndStop issue the same way (i removed the parent.)

Thanks a lot dude :thumb2: