|
-
Senior Member
AS3 Chracter Movement
Character movement is cake, well it was in AS2. I loved that good old onEnterFrame function days. Well I have to move on. I have the idea on what I want to do, but I'm stuck at what to do. I am writing my code on the time line ( i don't think that this is the best way to do it, but remembering first learning AS2 I used to write code in every movie clip, we learn). I know that I want to some how find out if a key is down and do an action(move).
so far I have a hero moving back and forward (the moving part is right but I want to control this guy)
Code:
/*** hero is a MovieClip in my Library ***/
var herox:hero = new hero();
var speedx:Number = 10;
function init() {
herox.addEventListener(Event.ENTER_FRAME,live);
herox.x=100;
herox.y=200;
}
function live(event:Event) {
if (herox.x>550) {
speedx=speedx*-1;
trace(herox.x);
}
if (herox.x<0) {
speedx=speedx*-1;
trace(herox.x);
}
herox.x+=speedx;
}
init();
addChild(herox);
now I can use addEventListener() and add a keyboardevent but if not like on enterframe its slow.
thanks a lot,
its been one day and I did a hello world, displayed a movieclip from the libary, and made it move. Thanks to you guys. progress is a beautiful thing.
CEO OF
-
Senior Member
Key.isDown RIP
For all the Googlers that will see this
After doing some more digging I found out that the Key.isDown() had been killed in AS3 for security reasons. It seems like AS3 is not so friendly to the gaming community. I haven't found an Explosion of AS3 games (like when flash 8 dropped ) maybe I haven't been looking in the right places. Maybe AS2 is still the best for game Development.
I found these workarounds:
AS3 Key.isDown Equivalent
this one is done aptly
AS3 Key.isDown in AS3
CEO OF
-
Omg, your right! I didn't realize that internal MC action script was removed, along with the Key.isDown() method! Holy ****, that's horrible! This is upgrade is definitely not good for AS newbies. I wish I could help you with this, but I am just barely becoming an advanced AS 2.0 programmer, and I personally don't see the need for AS 3.0 for even some of the more, intermediate applications.
But the first link seemed to have a good sample code, hope you can figure it out!
Knowledge is knowing a tomato is a fruit.
Wisdom is knowing not to put it in a fruit salad.
-
Senior Member
Interesting problem. I haven't done any games in AS3 but the way I do it -- and I won't speak to the speed of this, as press-response time has never been an issue with business apps -- is create several Function vars in the document class, for example:
Code:
enterFunction:Function = null;
escFunction:Function = null;
And then put a KeyboardEvent listener right after them:
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN,gotKeyDown);
function gotKeyDown(evt:KeyboardEvent) {
if (evt.keyCode == 13) {
if (this.enterFunction != null) {
this.enterFunction();
}
} else if (evt.keyCode == 27) {
if (this.escFunction != null) {
this.escFunction();
}
}
}
And then just re-define what root.enterFunction and root.escapeFunction do at various points in my code, or set them to null when they're not supposed to do anything. That saves me adding tons of listeners and relying on EnterFrames for events to fire. But then, I can't tell you that's any speedier than adding keyboard events whenever you need to; I can just tell you that it's probably safer than letting that event hang out in some movie clip somewhere...
Josh
-
Man, I used to admire flash for building a scripting language that when read out-load, made sense. But now, the fact that we need to know key codes and structure the document so that the script and the movieClips never touch, let along reside in each other, just makes things stressful. It's hard enough visualizing script effects in your head already...
-
Senior Member
I don't mind a hard scripting language, but what kills me about AS3 is that the whole method and implementation has changed. All the guru like abilities I had gained in as2 almost counts for nothing. If html 5 looked like it was C#, as a web designer I'd be pissed off. I am going to learn AS3 but its a kick in the butt to drop all the cool things I picked up in AS2. The migration from AS1 to AS2 was smooth as lubricated baby. I can't say the same for AS3.
The flash game developers have the best handled of the in house code. We had the best interaction in any flash genera . I don't feel that way now. I think that there will be an AS3.1 soon or I hope so.
I'm a middle minded person and flash is a middle mined environment were the coder and designer in me can co-exist. AS3 is too left mined. Yes AS3 is fast as something fast but it jumped from friendly to some what of a code Nazis.
I would really like to see some game examples done in flash cs3.
CEO OF
-
Your very much right. I checked out the samples provided with the program very disappointed. For one thing, I didn't see any AS3 examples that you couldn't do in AS2. For another, as far as a "faster" experience, I was yet to see higher performances. If AS3 really is so good that it should replace AS2, should they have provided examples that clearly show why AS2 should be replaced?
I think anyone who programs and designs in flash can get by just fine with AS2 and should have to conform to the new flash standards. Heck, most people haven't even delved into some of the extremely complex tricks that AS2 along could do. I've been using flash since MX, and I'm just barely discovering bitmap packages(Course, they probably weren't there in MX, but you get my point).
My ultimate point is that AS3 hasn't given me any worth-while reason to learn it. Even that whole speed thing, I mean, where in the program and in what situations is there this dramatic increase in processing speed? If anyone can tell me I might be able to appreciate it more, but I still think that, for the most part, speed is lost to inefficient programming, and not so much on the flash actionscript engine.
Well, whatever, I hope that you get something out of putting extra time into relearning AS. Good luck!
(PS: "workName" in my homework algorithm needs to be updated, sorry far all the people out there with fat blue-screens-of-death posted all over there computer due to an unused parameter. )
Last edited by SilverShockwave; 07-04-2007 at 01:34 AM.
Knowledge is knowing a tomato is a fruit.
Wisdom is knowing not to put it in a fruit salad.
-
hippie hater
wait until you find there is no perfect replacement for removeMovieClip
-
M.D.
so you're basically after enterFrame keys?
you need to create a Key class. Its really very simple, senocular's got one, probably can't do much better than what he has, its a really simple class. So using senoculars class:
http://www.kirupa.com/forum/showthre...69#post2098269
Code:
import flash.events.Event;
import flash.ui.Keyboard;
//Key should be local, no need to import, unless you change the package
Key.initialize(stage);
var herox:hero = new hero();
var speedx:Number = 10;
function init() {
herox.x = 100;
herox.y = 200;
addEventListener(Event.ENTER_FRAME, live);
}
function live(event:Event) {
if (Key.isDown(Keyboard.RIGHT) {
speedx = speedx;
}
else if (Key.isDown(Keyboard.LEFT)) {
speedx = -speedx;
}
herox.x += speedx;
}
init();
addChild(herox);
that should work
Cimmerian, just gotta be smart with your object, its not hard
Last edited by mr_malee; 07-04-2007 at 07:02 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|