PDA

View Full Version : Basic movement controls trouble!


ilollar
04-14-2005, 11:06 PM
I'm having trouble finding the help to write the simple code needed to control the character in a game I'm making. Keep in mind I'm an ultra newb to ActionScript, so I'm still figuring out what's what. Basically I have a movieclip of a character when he's static (not being moved) (called DaveStatic) and then a movie clip I want to show when he's being moved by the player (called DaveinFlight). I know how to code the movement so that a MC will move and turn in the appropriate direction, but I don't know how to write code so that we start with DaveStatic, when the down up button is pressed the clip changes to DaveinFlight (pointing and moving upwards), and then when the up button is released it changes back to DaveStatic.
If someone can help, I'd much appreciate it!
Thanks so much!

Gibberish
04-14-2005, 11:37 PM
you need to add keyListeners. So when the button is pressed you swap the laoded mc of daveStatic for daveInFlight. Then when the key is stopped being pressed you reverse it.

// ----- Key Listener
keyListener = new Object();
// on keyDown
keyListener.onKeyDown = function () {
if (Key.getCode() == Key.UP || Key.getCode() == Key.DOWN) {
//key is pressed load daveinFlight
trace("load daveInFlight");
}// End If Statements
}// End onKeyDown Function
// on keyUp
keyListener.onKeyUp = function () {
if (Key.getCode() == Key.UP || Key.getCode() == Key.DOWN) {
//key is released load daveStatic
trace("load daveStatic");
}// End If Statements
}// End onKeyUp Function

Key.addListener(keyListener);
// ----- End Key Listener

ilollar
04-14-2005, 11:47 PM
Sweet, but how do the MC's get to the stage? When I test this, it loads my movie clip, but the MC doesn't appear, let alone move around.

Sorry if these are dumb questions!

Thanks!!!

Gibberish
04-15-2005, 12:20 AM
copy and paste your code.

ilollar
04-15-2005, 12:26 AM
Here's what I had before posting:


watchMouse = new Object();
watchMouse.onMouseMove = function() {
trace("The mouse X position is "+_xmouse);
trace("The mouse Y position is "+_ymouse);
};
Mouse.addListener(watchMouse);

watchKeyBoard = new Object();
watchKeyBoard.onKeyDown = function() {
trace("a key is pressed. the key that is pressed is "+Key.getAscii());
};
Key.addListener(watchKeyBoard);
StageWidth = 800;
StageHeight = 600;
function moveStuff() {
if (Key.isDown(Key.LEFT)) {
this._x -= this.speed;
davestatic_mc._rotation = 90
}
if (Key.isDown(Key.RIGHT)) {
this._x += this.speed;
davestatic_mc._rotation = -90
}
if (Key.isDown(Key.UP)) {
this._y -= this.speed;
davestatic_mc._rotation = 180
}
if (Key.isDown(Key.DOWN)) {
this._y += this.speed;
davestatic_mc._rotation = 0
}
if (this._x>StageWidth+(this._width/2)) {
this._x = 0-(this._width/2);
}
if (this._x<0-(this._width/2)) {
this._x = StageWidth+(this._width/2);
}
if (this._y>StageHeight+(this._height/2)) {
this._y = 0-(this._height/2);
}
if (this._y<0-(this._height/2)) {
this._y = StageHeight+(this._height/2);
}
}
davestatic_mc.speed = 4;
davestatic_mc.onEnterFrame = moveStuff;

Gibberish
04-15-2005, 12:43 AM
you would want to have a holder mc for the daveMC's. I would have a blank mc called 'dave' then change the names of your tow other mc's to 'inFlight' and 'static'.

Then what you would use attachMovie() and load inFlight or statc into 'dave'. So in your code above you would do everything tot he 'dave' mc instead of davestatic_mc or daveInFlight_mc.

ilollar
04-15-2005, 01:06 AM
I get the theory, I just don't know how to apply it. I've made an empty MC called Dave. I placed that on the stage. I changed both the MC names and linkage names of the other MC's to Static and InFlight.
Now do I write actionscript code on the main timeline or on the actual dave MC? And what's the script. I know it involves attachMovie(), but I'm not sure on everything to write...

Thanks so much!!!

Gibberish
04-15-2005, 01:11 AM
if you want post a sample fla and I can code some stuff with comments for ya.

ilollar
04-15-2005, 01:14 AM
That would be awesome! I'm so sorry...I'm just a real newb to actionscript...trying to learn the ropes.

Thanks so much!

ilollar
04-15-2005, 10:08 PM
Sorry it took so long...here's a sample FLA. Thanks so much!!!

Gibberish
04-15-2005, 11:59 PM
try this out

ilollar
04-16-2005, 12:23 AM
Awesome! That's awesome! Your awesome!

Thanks so much man!

Out of curiousity...the MC's I'm using for the game are animated...the "inFlight" one consists of two frames. When I press and hold any key and Dave starts flying, those frames loop for a second but then stop. Any idea why?

Gibberish
04-16-2005, 12:33 AM
no idea. In the fla you provided the mc's were only one frame.

ilollar
04-16-2005, 12:37 AM
Yeah, I know...I had to do that because the size of my fla was too big to post with the orignal MC's. So i just made a temp one to give you. The real MC's i'm using are more than one frame. THe inFLight is only two frames, one of a small flame coming out a jet pac, and one of a bigger flame, looping to give the illusion of a jet pack. Before the code you gave me, if I just placed the MC on the stage, it would loop fine. But now it stops looping after about a second. Would this be caused by the code, or does it sound like a different problem?

ilollar
04-16-2005, 01:15 AM
If I just use the inFLight MC, the animation loops fine. But when the empty "Dave" MC is being used and the two MC are being switched, that animation stops looping when the key is pressed and held down. Any ideas why?