Copy link to clipboard
Copied
I want to make my object (chracter) move up,down,left,right...I wrote the following code, however it seem like its NOT working any help please??
In the library Instance name is : ch1 (its a movie clip)
Stage class name is: Movment
Following is the code...what am I doing wrong ??? Any suggestion or any idea???
also, I am getting compiler error saying: "Duplicate function defination"
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class Movement extends MovieClip
{
var ch1:Character;
public function Movement()
{
ch1 = new Character();
}
// Movement
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == keyboard.RIGHT)
{
vx = 5;
}
else if(event.keyCode == keyboard.UP)
{
vy = -5;
}
else if(event.keyCode == keyboard.DOWN)
{
vy = 5;
}
}
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT ||
event.keyCode == keyboard.RIGHT)
{
vx = 0;
}
else if (event.keyCode == Keyboard.DOWN ||
event.keycode == Keyboard.UP)
{
vy = 0;
}
}
//Move Character
function onEnterFrame(event:Event): void
{
// moves character
Character.x += vx;
Character.y += vy;
}
}
}
}
1 Correct answer
You have two functions named onKeyDown. You need to have different names for all functions in the class.
Copy link to clipboard
Copied
You have two functions named onKeyDown. You need to have different names for all functions in the class.
Copy link to clipboard
Copied
so under the first function called:
// Movement
function onKeyDown(event:KeyboardEvent):void
I can call or declare this function called:
function onKeyUp(event:KeyboardEvent):void
===============OR=====================
under the second function called: fuction onKeyDown(event:KeyboardEvent):void
I can replace that with: fuction onKeyUp(event:KeyboardEvent):void
Copy link to clipboard
Copied
Yes, you could do that. Try it and see which is the correct one to do it with if you cannot reason it out.
Copy link to clipboard
Copied
There are no event listeners added to keyboard and enter frame in this code. Where do you add event listeners?
Copy link to clipboard
Copied
After further examination it shows that the code you presented is so buggy - it shouldn't even compile.
Try this:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Movement extends MovieClip
{
private var ch1:Character;
private var vx:Number = 0;
private var vy:Number = 0;
public function Movement()
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
ch1 = new Character();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onKeyDown(e:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
vx = -5;
break;
case Keyboard.RIGHT:
vx = 5;
break;
case Keyboard.UP:
vy = -5;
break;
case Keyboard.DOWN:
vy = 5;
break;
}
}
private function onKeyUp(e:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
case Keyboard.RIGHT:
vx = 0;
break;
case Keyboard.UP:
case Keyboard.DOWN:
vy = 0;
break;
}
}
private function onEnterFrame(e:Event):void
{
ch1.x += vx;
ch1.y += vy;
}
}
}
Refactored conditionals to switches - they are faster.

