0
votes

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 :)

1
You need to take all the imports, vars (take out private/public keywords), and the code inside the constructor (the function whose name matches the class name) and put those by themselves at the top of your timeline code. Then copy paste the rest of the functions and just take out the private or public keywordsBadFeelingAboutThis
sorry, i was creating a Delivery Man flash game (as3), the codes on stage are as the first one, i found a better movement for the lorry_mc but it comes as external codes, i tried that with mine (externally) it works perfectly, but i have other codes like buttons and sound, as im not use working with external codes, there are lots of codes in my stage to "convert" them to external, so i thought maybe i should just "convert" the movement, and yea thats where my problem cameAdam Young

1 Answers

0
votes

Here is a quick example of how to convert a class file to timeline code:

Let's say this is your class file:

package {
    import flash.display.Sprite;
    import flash.events.Event;

    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 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);
        }
    }
}

The timeline equivalent would be:

import flash.display.Sprite;
import flash.events.Event;

var acceleration:Number=0.4;
var speed_decay:Number=0.80;
var rotation_step:Number=20;

//The Code from your class file constructor (the function whose name is the same as the class name) needs to be outside of a function when converted to timeline code
//this.x= someDefaultValue;
//this.y= someDefaultValue;
addEventListener(Event.ADDED_TO_STAGE,init);

//this could actually just be outside the function, since on timeline code runs AFTER the ADDED_TO_STAGE equivalent event
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);
}

What you do is copy the following:

  1. all the imports and put them at the top of your timeline code
  2. all the vars and put them after your imports (take out all public/private/protected keywords)
  3. take all the code inside the constructor (the function whose name exactly matches the class name) and put that under the vars (not inside a function)
  4. take the rest of the functions, and put them after everything else, taking out the public/private/protected keywords

Now, after you've moved from a class file to timeline code, you loose the ability to instantiate that class. So you will no longer be able to do something like: new car_mc() in other code.


All this said, there is no benefit to using timeline code over classes and I'm not sure why you would want to convert this to timeline code.

Ideally what you should do (if intent on using FlashPro and timelines) is attach these classes to library object. So if you have a car graphic in your library, right click it, go to properties, then in AS3 Linkage put in car_mc. The car_mc.as file should be in the same directory as the .fla.

Now whenever you drop in your car graphic, that class and it's code is attached to it. (same as if it was on the car_mc's timeline)