I have an annoying problem. I’ve googled a bit and can’t really find any good answers, so I’m hoping to find some help here.
I’m getting this error, which seems to be fairly common:
TypeError: Error #1006: update är inte en funktion. // = update is not a function (english)
at se.qmd.spaceInvaders::ShipHandler/onEnterFrame()
Update definently is a function, so I really can’t understand what the problem is.
My setup. I’m using Flashdevelop. I have a Main class, which is set as the document class in my fla file.
I have a package structure with my Main class in the src folder, and my subclasses in se.qmd.spaceInvaders and se.qmd.starLight. I also use TweenMax and keep it in the “normal” package structure from the src folder.
In the Main class I add to the stage an instance of the class LeGun – which is also an “exported” movieclip in the fla file, an instance of the class StarHandler and an instance of the class ShipHandler. I also have a keyboard listener which with the help of an enterframe function moves my instance of the LeGun class.
Fine, no problems. The StarHandler works like it should, and the ShipHandler works as it should up until i call 1 or 2 methods in the Ship class – which is also an “exported” movieclip in the fla file. The Ship class is of course the class that contains the Ship that is handled by the ShipHandler.
I’m including my ShipHandler and Ship classes below, and hope it doesn’t make the post excede some length.
ShipHandler.as :
package se.qmd.spaceInvaders {
import flash.display.MovieClip;
import se.qmd.spaceInvaders.Ship;
import flash.display.Stage;
import flash.events.Event;
public class ShipHandler extends MovieClip {
private var _shipArray:Array = [];
private const NUM_START_SHIPS:int = 25;
private var s:Stage = null;
private var _directionX:Number = 1;
private var _directionY:Number = 1;
private var _velocityX:Number;
private var _velocityY:Number;
private const YBORDER_TOP:int = 50;
private const YBORDER_BOTTOM:int = 525;
public function ShipHandler(o:Stage) {
s = o; // tar emot stage från main
createObjects();
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function createObjects():void {
for (var i:int = 0; i < NUM_START_SHIPS; i++) {
var aShip:Ship = new Ship();
aShip.x = Math.random() * (s.stageWidth - aShip.width) + aShip.width / 2;
aShip.y = Math.random() * (s.stageHeight - aShip.height -135) + aShip.height / 2 + 55;
this.addChild(aShip);
_shipArray.push(aShip);
aShip.startMove(Math.random(), Math.random());
}
}
private function onEnterFrame(e:Event):void {
for (var i:int = 0; i < _shipArray.length; i++) {
var aShip:Ship = _shipArray[i] as Ship;
aShip.update();
}
}
}
}
Ship.as :
package se.qmd.spaceInvaders {
import flash.display.MovieClip;
import flash.events.Event;
public class Ship extends MovieClip {
private var _directionX:Number = 1;
private var _directionY:Number = 1;
private var _velocityX:Number;
private var _velocityY:Number;
private const YBORDER_TOP:int = 50;
private const YBORDER_BOTTOM:int = 525;
public function Ship() {
//s = o; // tar emot stage
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
this.width = 50;
this.height = 18;
}
public function update():void {
// x led vänster kant
if(this.x <= this.width / 2) {
this.x = this.width / 2;
_directionX *= -1;
}
// x led höger kant
if(this.x >= 800 - this.width / 2) {
this.x = 800 - this.width/2;
_directionX *= -1;
}
// y led top kant
if(this.y <= YBORDER_TOP + this.height/2) {
this.y = YBORDER_TOP + this.height / 2;
_directionY *= -1;
}
// y led botten kant
if(this.y >= YBORDER_BOTTOM - this.height/2) {
this.y = YBORDER_BOTTOM - this.height / 2;
_directionY *= -1;
}
this.x += _velocityX * _directionX;
this.y += _velocityY * _directionY;
}
public function startMove(directionX:Number, directionY:Number):void {
trace(directionX, directionY);
_velocityX = 10;
_velocityY = 10;
if (directionX < 0.5) {
_directionX = -1;
}
if (directionY < 0.5) {
_directionY = -1;
}
}
public function changeDirection():void {
_directionX *= -1;
_directionY *= -1;
}
}
}
As soon as i call either the methods startMove or update from the ShipHandler, i get the error i wrote above.
Would appreciate any help as it seems to me I must be doing something fundamentally wrong…
It sounds to me like
Shipis just a MovieClip (which is a dynamic class and hence the compiler is not complaining when you’re trying to access a method that does not exist at compile-time.) MovieClips don’t have anupdate()method, so when you try to invoke it, it cannot be found on theShipinstance.Is the MovieClip in your FLA file really exported as
se.qmd.spaceInvaders.Ship(and not justShip)? If it’s justShip, that would explain why your written class is not assigned as the class of your ship graphics, leaving it to be just a simple MovieClip which would explain this error.