Rafah_CT
10-17-2011, 05:12 PM
I moved all the “movement” logic of my truck, form the truck class to a new public class called movement, the thing is that when I call for this class public functions, flash tells me Error #1180: Call to a possibly undefined method..
The reason I want to create a whole separate class that manages movement is because I want to later implement an auto/manual driving system, and some logic that makes the background scroll to the opposite direction of the truck when the truck reaches the corners of the swf, but that’s not relevant at the moment, so:
Movement Class:
package
{
public class Movement
{
public var speed:Number = 0;
public var speedMax:Number = 8;
public var speedMaxReverse:Number = -3;
public var speedAcceleration:Number = .15;
public var speedDeceleration:Number = .90;
public var groundFriction:Number = .95;
public var steering:Number = 0;
public var steeringMax:Number = 2;
public var steeringAcceleration:Number = .10;
public var steeringFriction:Number = .98;
public var velocityX:Number = 0;
public var velocityY:Number = 0;
public function Movement()
{
speed *= groundFriction;
if (speed > 0 && speed < 0.05)
{
speed = 0;
}
velocityX = Math.sin (Truck.main.rotation * Math.PI / 180) * speed;
velocityY = Math.cos (Truck.main.rotation * Math.PI / 180) * -speed;
Truck.main.x += velocityX;
Truck.main.y += velocityY;
if (steering > 0)
{
if (steering < 0.05)
{
steering = 0;
}
}
else if (steering < 0)
{
if (steering > -0.05)
{
steering = 0;
}
}
steering = steering * steeringFriction;
steering -= (steering * 0.1);
Truck.main.rotation += steering * speed;
}
public function moveUp()
{
if (speed < speedMax)
{
speed += speedAcceleration;
if (speed > speedMax)
{
speed = speedMax;
}
}
}
public function moveDown()
{
if (speed > speedMaxReverse)
{
speed -= speedAcceleration;
if (speed < speedMaxReverse)
{
speed = speedMaxReverse;
}
}
}
public function moveRight()
{
steering += steeringAcceleration;
if (steering < -steeringMax)
{
steering = steeringMax;
}
}
public function moveLeft()
{
steering -= steeringAcceleration;
if (steering > steeringMax)
{
steering = steeringMax;
}
}
}
}
Truck Class:
package
{
import flash.display.MovieClip;
import flash.events.*
import flash.ui.Keyboard;
public class Truck extends MovieClip
{
static public var main;
public var xBuffer:int;
public var yBuffer:int;
public function Truck()
{
xBuffer = 50;
yBuffer = 50;
main = this;
addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
}
private function onAddToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);
init();
}
private function init():void
{
addEventListener(Event.ENTER_FRAME, enterFrame);
}
private function enterFrame(event:Event):void
{
Movement();
if(Key.isDown(Keyboard.LEFT) && x > xBuffer)
{
moveLeft();
}
else if(Key.isDown(Keyboard.RIGHT) && x < Game.WIDTH-xBuffer)
{
moveRight();
}
if(Key.isDown(Keyboard.UP) && y > yBuffer)
{
moveUp();
}
else if(Key.isDown(Keyboard.DOWN) && y < Game.HEIGHT-yBuffer)
{
moveDown();
}
}
}
}
____
I originally requested help at kongregate, but the I didn't understand the tips they gave me and since it took the guy who answered 12 hours to answer I'm asking for help in here, I'm posting what he told me to do and my key class and my document class..
package
{
import flash.display.*
public class Game extends MovieClip
{
static public var main;
static public var WIDTH:int = 700;
static public var HEIGHT:int = 600;
public var movement:Movement;
public var truck:Truck;
public var background:Background;
public function Game()
{
main = this;
Key.initialize(stage);
background = new Background();
addChild(background);
truck = new Truck();
truck.x = WIDTH/2;
truck.y = HEIGHT/2;
addChild( truck );
}
}
}
package
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Key
{
private static var initialized:Boolean = false;
private static var keysDown:Object = new Object();
public static function initialize(stage:Stage)
{
if (!initialized)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
initialized = true;
}
}
public static function isDown(keyCode:uint):Boolean
{
return Boolean(keyCode in keysDown);
}
private static function keyPressed(event:KeyboardEvent):void
{
keysDown[event.keyCode] = true;
}
private static function keyReleased(event:KeyboardEvent):void
{
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
private static function clearKeys(event:Event):void
{
keysDown = new Object();
}
}
}
(I didn't make this key class, I got it from a game's source code, I understand most of it(this is in case you say "but if you coded that key class you should know how to solve your problem))"
@Rafael:
So you refactored your movement into its own class? Great.
But why the heck did you hardcode the “Truck” into it? That breaks pretty much everything.
Stop abusing the static keyword.
Your Movement Class needs a reference to a DisplayObject it is supposed to move.
to do this, pass the reference to the constructor of the Movement class.
Your Truck should never react on Key-presses, it is just an graphical asset, keep logic and visuals separated.
In your Main class (aka document class):
- create a new Truck object, addChild it
- create a Movement Object, pass the reference to the Truck
- change variables of the Movement Object when a Key is pressed (in the Main class)
I replied:
I hardcoded truck into because it said it had no X or Y values.
I make the truck class react to the keys because when I extend my game, truck will be the class that governs all the truck clips that I have on my game, truck itself won’t do anything to a graphic, I believe I’m just thinking ahead of time when I do this.
In any case I am lost in how to do this: " pass the reference to the Truck"
Also, when you say make them static, you refer to the speed variables?
He just posted:
private var _target:DisplayObject;
public function Movement(target:DisplayObject)
{
_target = target;
I understand nothing from his tips, I'm hoping you can either explain them to me since he seems reluctant to do so, or just give new advice (the more details the better)
Sorry for the bother and any inconvinience.
The reason I want to create a whole separate class that manages movement is because I want to later implement an auto/manual driving system, and some logic that makes the background scroll to the opposite direction of the truck when the truck reaches the corners of the swf, but that’s not relevant at the moment, so:
Movement Class:
package
{
public class Movement
{
public var speed:Number = 0;
public var speedMax:Number = 8;
public var speedMaxReverse:Number = -3;
public var speedAcceleration:Number = .15;
public var speedDeceleration:Number = .90;
public var groundFriction:Number = .95;
public var steering:Number = 0;
public var steeringMax:Number = 2;
public var steeringAcceleration:Number = .10;
public var steeringFriction:Number = .98;
public var velocityX:Number = 0;
public var velocityY:Number = 0;
public function Movement()
{
speed *= groundFriction;
if (speed > 0 && speed < 0.05)
{
speed = 0;
}
velocityX = Math.sin (Truck.main.rotation * Math.PI / 180) * speed;
velocityY = Math.cos (Truck.main.rotation * Math.PI / 180) * -speed;
Truck.main.x += velocityX;
Truck.main.y += velocityY;
if (steering > 0)
{
if (steering < 0.05)
{
steering = 0;
}
}
else if (steering < 0)
{
if (steering > -0.05)
{
steering = 0;
}
}
steering = steering * steeringFriction;
steering -= (steering * 0.1);
Truck.main.rotation += steering * speed;
}
public function moveUp()
{
if (speed < speedMax)
{
speed += speedAcceleration;
if (speed > speedMax)
{
speed = speedMax;
}
}
}
public function moveDown()
{
if (speed > speedMaxReverse)
{
speed -= speedAcceleration;
if (speed < speedMaxReverse)
{
speed = speedMaxReverse;
}
}
}
public function moveRight()
{
steering += steeringAcceleration;
if (steering < -steeringMax)
{
steering = steeringMax;
}
}
public function moveLeft()
{
steering -= steeringAcceleration;
if (steering > steeringMax)
{
steering = steeringMax;
}
}
}
}
Truck Class:
package
{
import flash.display.MovieClip;
import flash.events.*
import flash.ui.Keyboard;
public class Truck extends MovieClip
{
static public var main;
public var xBuffer:int;
public var yBuffer:int;
public function Truck()
{
xBuffer = 50;
yBuffer = 50;
main = this;
addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
}
private function onAddToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);
init();
}
private function init():void
{
addEventListener(Event.ENTER_FRAME, enterFrame);
}
private function enterFrame(event:Event):void
{
Movement();
if(Key.isDown(Keyboard.LEFT) && x > xBuffer)
{
moveLeft();
}
else if(Key.isDown(Keyboard.RIGHT) && x < Game.WIDTH-xBuffer)
{
moveRight();
}
if(Key.isDown(Keyboard.UP) && y > yBuffer)
{
moveUp();
}
else if(Key.isDown(Keyboard.DOWN) && y < Game.HEIGHT-yBuffer)
{
moveDown();
}
}
}
}
____
I originally requested help at kongregate, but the I didn't understand the tips they gave me and since it took the guy who answered 12 hours to answer I'm asking for help in here, I'm posting what he told me to do and my key class and my document class..
package
{
import flash.display.*
public class Game extends MovieClip
{
static public var main;
static public var WIDTH:int = 700;
static public var HEIGHT:int = 600;
public var movement:Movement;
public var truck:Truck;
public var background:Background;
public function Game()
{
main = this;
Key.initialize(stage);
background = new Background();
addChild(background);
truck = new Truck();
truck.x = WIDTH/2;
truck.y = HEIGHT/2;
addChild( truck );
}
}
}
package
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Key
{
private static var initialized:Boolean = false;
private static var keysDown:Object = new Object();
public static function initialize(stage:Stage)
{
if (!initialized)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
initialized = true;
}
}
public static function isDown(keyCode:uint):Boolean
{
return Boolean(keyCode in keysDown);
}
private static function keyPressed(event:KeyboardEvent):void
{
keysDown[event.keyCode] = true;
}
private static function keyReleased(event:KeyboardEvent):void
{
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
private static function clearKeys(event:Event):void
{
keysDown = new Object();
}
}
}
(I didn't make this key class, I got it from a game's source code, I understand most of it(this is in case you say "but if you coded that key class you should know how to solve your problem))"
@Rafael:
So you refactored your movement into its own class? Great.
But why the heck did you hardcode the “Truck” into it? That breaks pretty much everything.
Stop abusing the static keyword.
Your Movement Class needs a reference to a DisplayObject it is supposed to move.
to do this, pass the reference to the constructor of the Movement class.
Your Truck should never react on Key-presses, it is just an graphical asset, keep logic and visuals separated.
In your Main class (aka document class):
- create a new Truck object, addChild it
- create a Movement Object, pass the reference to the Truck
- change variables of the Movement Object when a Key is pressed (in the Main class)
I replied:
I hardcoded truck into because it said it had no X or Y values.
I make the truck class react to the keys because when I extend my game, truck will be the class that governs all the truck clips that I have on my game, truck itself won’t do anything to a graphic, I believe I’m just thinking ahead of time when I do this.
In any case I am lost in how to do this: " pass the reference to the Truck"
Also, when you say make them static, you refer to the speed variables?
He just posted:
private var _target:DisplayObject;
public function Movement(target:DisplayObject)
{
_target = target;
I understand nothing from his tips, I'm hoping you can either explain them to me since he seems reluctant to do so, or just give new advice (the more details the better)
Sorry for the bother and any inconvinience.