PDA

View Full Version : Copied code, still doesnt work?


KentXandir
04-09-2012, 01:43 PM
I've been trying very hard to do actionscript, slowly by myself by watching videos. So far, I've had no trouble. I've figured out the use of variables, functions and some other very simple stuff and I was watching a video that I can't link to cause I might BE A SPAMMER! (OMG) to get an idea how movement works. I think I understand... but I must be doing something wrong. Something so simple it doesn't warrant mentioning in the video. After typing the code myself, along with the guy in the video, and it not working, I even copied it directly from his video description and it /still/ isn't working. I wonder if it has something to do with saving the file or maybe a class? (I've only barely read about classes) Anyone have an idea of what might be wrong here? >_< I feel like an idioto! Because I can't link to the video, I'll just paste the code below, although I know the code works (comments in the video say so, and he demonstrates it in the video)

It also may be worth mentioning that I don't get any errors? It's a pretty short code. Thanks in advance!

Edit: I did create the Char file as a movieclip, and exported it for actionscript, left the class name as Char and didn't edit anything else. Just in case someone might think I didn't do that. x.x

Code:

import flash.events.KeyboardEvent;
import flash.events.Event;

var char:Char = new Char();
char.x = 250;
char.y = 255;
addChild(char);

var speed:Number = 5;
var leftButton:Boolean = false;
var rightButton:Boolean = false;
var upButton:Boolean = false;
var downButton:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
stage.addEventListener(KeyboardEvent.KEY_UP, kUp);
stage.addEventListener(Event.ENTER_FRAME, moveChar);

function kDown(event:KeyboardEvent){
if(event.keyCode == 37 || event.keyCode == 65){
leftButton = true;
}
if(event.keyCode == 38 || event.keyCode == 87){
upButton = true;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightButton = true;
}
if(event.keyCode == 40 || event.keyCode == 83){
downButton = true;
}
}
function kUp(event:KeyboardEvent){
if(event.keyCode == 37 || event.keyCode == 65){
leftButton = false;
}
if(event.keyCode == 38 || event.keyCode == 87){
upButton = false;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightButton = false;
}
if(event.keyCode == 40 || event.keyCode == 83){
downButton = false;
}
}
function moveChar(event:Event){
if(leftButton == true){
char.x -= speed;
}
else if(rightButton == true){
char.x += speed;
}
else if(upButton == true){
char.y -= speed;
}
else if(downButton == true){
char.y += speed;
}
}

tacos
04-09-2012, 08:07 PM
To post a link, before you have enough posts to do so, just omit the http://www. part when posting it... ;)

KentXandir
04-09-2012, 09:20 PM
Ahh, thank you. Although now I've figured it out, I can actually move my object around! now I'm trying to figure out how to go about creating different areas you can enter/leave. I swear this is much more complicated than most flash games would lend you to believe!

My original thought was to use collision detection (something I've also not yet played around with) with doors or even floor panels (invisible in the flash) but I'm not sure how to do this or if theres an easier way or what.

xdeath
04-12-2012, 08:31 AM
its really just like you said. you need to know what action is going to take you to the next level. so perhaps:
- maybe moving into an object like a goal or a door.
- or in some games you simply walk off the screen.

so you simply check if that happens, and if it does happen then you remove all the objects within the level and then add the ones for the next screen/level. there shouyld be more then enough examples of doing collisions on google.