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.