hi i am new to actionscript 3.0, i need help in "converting"/replace these codes that i create on stage :
stage.addEventListener(KeyboardEvent.KEY_DOWN, whichKey)
function whichKey(event:KeyboardEvent): void
{
if (event.keyCode == Keyboard.LEFT){
lorry_mc.x = lorry_mc.x - 5;
lorry_mc.rotation = -180;
if( road.walls.hitTestPoint(lorry_mc.x,lorry_mc.y,true) ){
lorry_mc.x += 5;
}
if( road.walls.hitTestPoint(lorry_mc.x,lorry_mc.y+height,true) ){
lorry_mc.x += 5;
}
}
if (event.keyCode == Keyboard.RIGHT){
lorry_mc.x = lorry_mc.x + 5;
lorry_mc.rotation = 0;
if( road.walls.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y,true) ){
lorry_mc.x -= 5;
}
if( road.walls.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y+height,true) ){
lorry_mc.x -= 5;
}
if (level1target2_mc.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y+lorry_mc.height,true)){
chairbottom_mc.visible=false;
chairtop_mc.visible=false;
score++;
score_txt.text = String(score);
}
if (brokenroad_mc.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y+lorry_mc.height,true)){
gotoAndPlay('gameoverframe')
}
}
time1.start();
if (event.keyCode == Keyboard.UP){
lorry_mc.y = lorry_mc.y - 5;
lorry_mc.rotation = -90;
if( road.walls.hitTestPoint(lorry_mc.x,lorry_mc.y,true) ){
lorry_mc.y += 5;
}
if( road.walls.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y,true) ){
lorry_mc.y += 5;
}
if (level1target1_mc.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y+lorry_mc.height,true)){
cupboardbottom_mc.visible=false;
cupboardtop_mc.visible=false;
score++;
score_txt.text = String(score);
}
}
if (event.keyCode == Keyboard.DOWN){
lorry_mc.y = lorry_mc.y + 5;
lorry_mc.rotation = 90;
if(road.walls.hitTestPoint(lorry_mc.x,lorry_mc.y+lorry_mc.height,true) ){
lorry_mc.y -= 5;
}
if(road.walls.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y+lorry_mc.height,true) ){
lorry_mc.y -= 5;
}
if (level1target3_mc.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y+lorry_mc.height,true)){
freezerbottom_mc.visible=false;
freezertop_mc.visible=false;
score++;
score_txt.text = String(score);
}
if (level1target2_mc.hitTestPoint(lorry_mc.x+lorry_mc.width,lorry_mc.y+lorry_mc.height,true)){
chairbottom_mc.visible=false;
chairtop_mc.visible=false;
score++;
score_txt.text = String(score);
}
}
if (score==3) {
time1.stop();
gotoAndPlay('resultlorryframe')
}
};
with this one without the "public var" etc :
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Point;
public class car_mc extends Sprite {
public var acceleration:Number=0.4;
public var speed_decay:Number=0.80;
public var rotation_step:Number=20;
public var max_speed:Number=10;
public var back_speed:Number=1;
public var speed:Number=0;
public var accelerate,brake,turn_left,turn_right:Boolean=false;
public function car_mc(posx:int,posy:int):void {
x=posx;
y=posy;
addEventListener(Event.ADDED_TO_STAGE,init);
}
public function init(e:Event):void {
addEventListener(Event.ENTER_FRAME,on_enter_frame);
stage.addEventListener(KeyboardEvent.KEY_DOWN,on_key_down);
stage.addEventListener(KeyboardEvent.KEY_UP,on_key_up);
}
public function on_enter_frame(e:Event):void {
if (accelerate&&speed<max_speed) {
speed+=acceleration;
}
if (brake&&speed>-1) {
speed-=back_speed;
}
var speed_x:Number=Math.sin(rotation*0.0174532925)*speed;
var speed_y:Number=- Math.cos(rotation*0.0174532925)*speed;
if (turn_left) {
rotation -= rotation_step*(speed/max_speed);
}
if (turn_right) {
rotation += rotation_step*(speed/max_speed);
}
y+=speed_y;
x+=speed_x;
var point_left:Point=new Point(-9,0);
var point_right:Point=new Point(9,0);
var point_front:Point=new Point(0,-13);
var point_back:Point=new Point(0,13);
point_left=localToGlobal(point_left);
point_right=localToGlobal(point_right);
point_front=localToGlobal(point_front);
point_back=localToGlobal(point_back);
var par:racing=this.parent as racing;
if (par.ground.hitTestPoint(point_left.x,point_left.y,true)) {
rotation+=5;
speed*=0.85;
}
if (par.ground.hitTestPoint(point_right.x,point_right.y,true)) {
rotation-=5;
speed*=0.85;
}
if (par.ground.hitTestPoint(point_front.x,point_front.y,true)) {
speed*=0.55;
}
if (par.ground.hitTestPoint(point_back.x,point_back.y,true)) {
speed*=0.55;
}
if (Math.abs(speed)>0.3) {
speed*=speed_decay;
} else {
speed=0;
}
}
public function on_key_down(e:KeyboardEvent):void {
if (e.keyCode==38) {
accelerate=true;
}
if (e.keyCode==40) {
brake=true;
}
if (e.keyCode==37) {
turn_left=true;
}
if (e.keyCode==39) {
turn_right=true;
}
}
public function on_key_up(e:KeyboardEvent):void {
if (e.keyCode==38) {
accelerate=false;
}
if (e.keyCode==40) {
brake=false;
}
if (e.keyCode==37) {
turn_left=false;
}
if (e.keyCode==39) {
turn_right=false;
}
}
}
}
as im not use working with external script, found this example on google which the movement is really smooth and nicer than mine.
EDITED : the second coding above came with these codes in another external file :
package {
import flash.display.Sprite;
public class racing extends Sprite {
public var car:car_mc;
public var ground:ground_mc = new ground_mc();
public function racing():void {
car = new car_mc(125,220);
addChild(ground);
addChild(car);
}
}
}
i will email my .fla and .swf if you want Thanks in advance :)