I have looked through this code over and over and over but just cant seem to find where the problem is rooted. Here is the error message:
"TypeError: Error #1009:
at com.senocular.utils::KeyObject/construct()[C:\Users\nti\Desktop\Ship game\com\senocular\utils\KeyObject.as:29]
at com.senocular.utils::KeyObject()[C:\Users\nti\Desktop\Ship game\com\senocular\utils\KetyObject.as:23]
at com.asgamer.basics1::Ship()[C:\Users\nti\Desktop\Ship game\com\asgamer\basics1\Ship.as:24]
at com.asgamer.basics1::Engine()[C:\Users\nti\Desktop\Ship game\com\asgamer\basics1\Engine.as:17]
"
It's certainly not in the KeyObject class since that's downloaded and not changed in any(and has worked before), but here's that snippet of code anyway(notice the markers where the lines are):
dynamic public class KeyObject extends Proxy {
private static var stage:Stage;
private static var keysDown:Object;
public function KeyObject(stage:Stage) {
construct(stage); <---------------------------------------------LINE 23
}
public function construct(stage:Stage):void {
KeyObject.stage = stage;
keysDown = new Object();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); <--- LINE 29
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
Here are the parts of code that's causing the error:
package com.asgamer.basics1
{
import flash.display.MovieClip;
import flash.display.Stage;
import com.senocular.utils.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
public class Ship extends MovieClip
{
private var stageRef:Stage;
private var key:KeyObject;
private var speed:Number = 0.5;
private var vx:Number = 0;
private var vy:Number = 0;
private var friction:Number = 0.93;
private var maxspeed:Number = 8;
public function Ship(stageRef:Stage)
{
this.stageRef = stageRef;
key = new KeyObject(stageRef); <----------------------------- LINE 24
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
and here's the engine class:
package com.asgamer.basics1
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Engine extends MovieClip
{
private var numStars:int = 80;
private var enemyList:Array = new Array();
private var ourShip:Ship;
public function Engine() : void
{
ourShip = new Ship(stage); <------------------------------- LINE 17
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
stage.addChild(ourShip);
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
I would really, really appreciate if someone would answer since this project is crucial to my final grades!
Instance of the Engine class is being constructed at the moment, when the stage property is still null. This means that the Engine class instance hasn't been added to the display list yet.
See: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#stage
Related
I have my main class where I add EventLisitener to Sprites in other classes Like so:
public function Main() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
addChild(Menu.menuBackground);
addChild(Menu.startButton);
//Adds event Listener to Menu's Sprite startButton.
Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
}
This all works Fine and Dandy but later I try to do it again, the same way. But, clicking on the sprites does nothing. Here is the full Main Class code. Along with the code for the other three Sprite classes.
Full Main:
package {
//Other Files
import Menu;
import CrossHair;
import Birds;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends Sprite {
//Game values
public static var gameWidth:int = 750;
public static var gameHeight:int = 750;
[Embed (source = "lib/background.png")]
public var backgroundClass:Class;
public var background:Bitmap = new backgroundClass();
public function Main() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
addChild(Menu.menuBackground);
addChild(Menu.startButton);
Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
}
//Function starts game
public function startGame(Evt:Event):void {
Mouse.hide(); //Hides Mouse
removeChild(Menu.startButton); //Get rid of startButton
Menu.startButton.removeEventListener(MouseEvent.CLICK, startGame);
reRender();
//Add eventListiners
addEventListener(Event.ENTER_FRAME, update);
Birds.bird.addEventListener(MouseEvent.CLICK, Birds.shot);
EnterShopButton.shopButton.addEventListener(MouseEvent.CLICK, enterShop);
}
public function reRender():void {
addChild(background); //Add background
addChild(Birds.bird); //Add birds.
addChild(EnterShopButton.shopButton); //Add UpgradeMenuButton
addChild(CrossHair.crossHair); //Add crosshair
}
public function enterShop():void {
stage.removeChildren(); //Removes all children from stage.
}
public function update(evt:Event):void {
Birds.update();
CrossHair.crossHair.x = mouseX - (CrossHair.crossHairImg.width / 2);
CrossHair.crossHair.y = mouseY - (CrossHair.crossHairImg.height / 2);
}
}
}
Event Lisitners:
package {
//Other files
import flash.events.Event;
import Main;
import flash.display.Sprite;
import flash.display.Bitmap;
public class Menu extends Sprite {
//create menbu background Bitmap
[Embed (source = "lib/menubackground.png")]
public static var menuBackgroundClass:Class;
public static var menuBackground:Bitmap = new menuBackgroundClass();
//create startButton Bitmap
[Embed (source = "lib/menustartbutton.png")]
public static var startButtonClass:Class;
public static var startButtonImg:Bitmap = new startButtonClass();
public static var startButton:Sprite = new Sprite();
//Set startButton's values
startButton.addChild(startButtonImg);
startButton.x = (Main.gameWidth / 2) - (startButtonImg.width / 2);
startButton.y = (Main.gameHeight / 2) - (startButtonImg.height / 2);
}
}
package {
//Other files
import Main;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.events.MouseEvent;
public class Birds extends Sprite {
public static var xSpeed:int = 10;
public static var ySpeed:int = 10;
public static var dead:Boolean = false;
//Create bird Sprite
[Embed (source = "lib/bird.png")]
public static var birdClass:Class;
[Embed (source = "lib/birdead.png")]
public static var deadBirdClass:Class;
public static var birdImg:Bitmap = new birdClass();
public static var deadBirdImg:Bitmap = new deadBirdClass();
public static var bird:Sprite = new Sprite();
//Sets Sprite's values
bird.addChild(birdImg);
bird.buttonMode = true;
bird.x = 0;
bird.y = 0;
public static function update():void {
bird.x += Math.random() * xSpeed;
bird.y += Math.random() * ySpeed;
if (!dead) {
if (bird.x >= (Main.gameWidth - birdImg.width) || bird.x <= 0) {
xSpeed = xSpeed * -1;
}
if (bird.y >= (Main.gameHeight - birdImg.height) || bird.y <= 0) {
ySpeed = ySpeed * -1;
}
} else {
if (bird.y > (Main.gameHeight - deadBirdImg.height)) {
resetBird();
}
}
}
public static function shot(evt:MouseEvent):void {
if (!dead) {
bird.removeChild(birdImg);
bird.addChild(deadBirdImg);
dead = true;
xSpeed = 0;
ySpeed = 50;
}
}
public static function resetBird():void {
bird.removeChild(deadBirdImg);
bird.addChild(birdImg);
dead = false;
bird.x = 0
bird.y = 0;
xSpeed = 10;
ySpeed = 10;
}
}
}
package {
//Other Files
import Main;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.MouseEvent;
public class EnterShopButton extends Sprite{
//Create crossHair
[Embed (source = "lib/shopbutton.png")]
public static var shopButtonClass:Class;
public static var shopButtonImg:Bitmap = new shopButtonClass();
public static var shopButton:Sprite = new Sprite();
//Set CrossHair's values
shopButton.addChild(shopButtonImg);
shopButton.buttonMode = true;
shopButton.x = Main.gameWidth - shopButtonImg.width;
shopButton.y = Main.gameHeight - shopButtonImg.height;
}
}
Same problem, same answer. Your previous question was answered the same way but you still persist on doing the exact same thing and getting into the exact same problem.
Stop trying to run code in static scope! addChild can't work in that scope and any other type of code.
Not sure why I was voted down. There's not much to answer here since the same problem with the same user was answered in another post.
This is really not difficult, user wants to run code at class definition scope. At class definition scope you can instantiate variable yes but that's all you can do. User wants to run code and logic at that scope and this is just completely ignored by the compiler. This was already pointed out to him in his previous question and he was even given the advice to stop trying to run code at that scope to avoid further problem. This is really simple to understand:
[Embed (source = "lib/menubackground.png")]
public static var menuBackgroundClass:Class;
public static var menuBackground:Bitmap = new menuBackgroundClass();
//instantiation is allowed at that scope and will work
//create startButton Bitmap
[Embed (source = "lib/menustartbutton.png")]
public static var startButtonClass:Class;
public static var startButtonImg:Bitmap = new startButtonClass();
//instantiation is allowed at that scope and will work
public static var startButton:Sprite = new Sprite();
//instantiation is allowed at that scope and will work
//COMPILER WILL IGNORE AT THAT SCOPE ANY CODE THAT IS NOT INSTANTIATION
//SO ALL FOLLOWING LINES ARE IGNORED AND PRODUCE NO RESULT
//EVEN TRACING WOULD NOT WORK HERE
// (Main.gameWidth / 2) WILL NOT BE CALCULATED AND DOES NOT PRODUCE ANY VALUE
// startButton.x WILL NOT BE SET AND WILL STAY AT DEFAULT VALUE 0
// FINALLY startButton WILL NOT addChild ANYTHING.
//ANY FORM OF CODE LOGIC IS IGNORED AT THAT SCOPE AND WILL NEVER PRODUCE ANY RESULT
startButton.addChild(startButtonImg);
startButton.x = (Main.gameWidth / 2) - (startButtonImg.width / 2);
startButton.y = (Main.gameHeight / 2) - (startButtonImg.height / 2);
I'm trying to use vectors to make an enemy track a target (player's ship). However, I keep getting Error 1046. Any advice? The error occurs on the line containing the drawForceVector function in the Game.as file.
Here is my Game.as file code:
package{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.text.TextField;
public class Game extends MovieClip
{
public static var mouse :Vector3D = new Vector3D(100, 100);
public var boids :Vector.<Boid> = new Vector.<Boid>;
public var forces :Sprite;
public function Game() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e :Event) :void {
var i :int, boid :Boid;
for (i = 0; i < 2; i++) {
boid = new Boid(stage.stageWidth * Math.random(), stage.stageHeight * Math.random(), 20 + Math.random() * 20);
addChild(boid);
boids.push(boid);
}
forces = new Sprite();
addChild(forces);
}
public function update():void {
var i :int;
forces.graphics.clear();
for (i = 0; i < boids.length; i++) {
boids[i].update();
drawForces(boids[i]);
}
}
private function drawForces(boid :Boid) :void {
var desired :Vector3D = boid.desired.clone();
var velocity :Vector3D = boid.velocity.clone();
var steering :Vector3D = boid.steering.clone();
velocity.normalize();
desired.normalize();
steering.normalize();
// Force vectors
drawForceVector(boid, velocity, 0x00FF00);
drawForceVector(boid, desired, 0x454545);
drawForceVector(boid, steering, 0x0000FF);
// Target
forces.graphics.lineStyle(1, 0x323232);
forces.graphics.beginFill(0x323232);
forces.graphics.drawCircle(mouse.x, mouse.y, 10);
forces.graphics.endFill();
}
private function drawForceVector(boid :Boid, force :Vector3D, color :uint, scale :Number = 100) :void { //ERROR OCCURS ON THIS LINE
forces.graphics.moveTo(boid.x + boid.width / 2, boid.y + boid.height / 2);
forces.graphics.lineStyle(2, color);
forces.graphics.lineTo(boid.x + force.x * scale, boid.y + force.y * scale);
}
}
}
Boid.as Code:
package {
import flash.display.MovieClip;
import flash.geom.Vector3D;
import flash.object.Vector3D;
public class Boid extends MovieClip
{
public static const MAX_FORCE :Number = 0.4;
public static const MAX_VELOCITY :Number = 3;
public var position :Vector3D;
public var velocity :Vector3D;
public var target :Vector3D;
public var desired :Vector3D;
public var steering :Vector3D;
public var mass :Number;
public function Boid(posX :Number, posY :Number, totalMass :Number = 20) {
position = new Vector3D(posX, posY);
velocity = new Vector3D(-1, -2);
target = new Vector3D(310, 240);
desired = new Vector3D(0, 0);
steering = new Vector3D(0, 0);
mass = totalMass;
truncate(velocity, MAX_VELOCITY);
x = position.x;
y = position.y;
graphics.beginFill(0xFF0000);
graphics.drawRect(0, 0, 20, 20);
graphics.endFill();
}
private function seek(target :Vector3D) :Vector3D {
var force :Vector3D;
desired = target.subtract(position);
desired.normalize();
desired.scaleBy(MAX_VELOCITY);
force = desired.subtract(velocity);
return force;
}
public function truncate(vector :Vector3D, max :Number) :void {
var i :Number;
i = max / vector.length;
i = i < 1.0 ? 1.0 : i;
vector.scaleBy(i);
}
public function update():void {
target = Game.mouse;
steering = seek(target);
truncate(steering, MAX_FORCE);
steering.scaleBy(1 / mass);
velocity = velocity.add(steering);
truncate(velocity, MAX_VELOCITY);
position = position.add(velocity);
x = position.x;
y = position.y;
}
}
}
Main.as Code:
package {
import flash.display.Sprite;
import flash.events.*;
[SWF(width = "600", height = "480")]
public class Main extends Sprite
{
private var game :Game = new Game();
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stage.frameRate = 30;
addChild(game);
}
private function mouseMove(e :MouseEvent) :void {
Game.mouse.x = e.stageX;
Game.mouse.y = e.stageY;
}
private function enterFrame(e :Event) :void {
game.update();
}
}
}
Thank you very much.
The target version of the player must be higher than the one you've got - Flash Player 9. As seen by the reference, it's said that Vector3D is included in: Runtime Versions: Flash Player 10, AIR 1.5. So basically you are building for so old Flash Player, that it still doesn't have this class inside it.
Simply change the target version to the latest possible one, and be sure it's higher than Flash Player 10. That will do the job! :)
I'm making an AS3 platform game. I don't know why but it won't go to full screen...
Here's my code :
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.getTimer;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
public class PlatformGame extends MovieClip {
// movement constants
static const gravity:Number = .004;
// screen constants
static const edgeDistance:Number = 100;
// object arrays
private var fixedObjects:Array;
private var otherObjects:Array;
// hero and enemies
private var hero:Object;
private var enemies:Array;
// game state
private var playerObjects:Array;
private var gameScore:int;
private var gameMode:String = "start";
private var playerLives:int;
private var lastTime:Number = 0;
// start game
public function startPlatformGame() {
addEventListener(Event.ADDED_TO_STAGE, startGame);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
playerObjects = new Array();
gameScore = 0;
gameMode = "play";
playerLives = 3;
}
Do you know what could be the problem ? (I've just put the begining if my code, if you think it's necessary that I put all my code, don't hesitate to tell me)
Read this Adobe documentation:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS2E9C7F3B-6A7C-4c5d-8ADD-5B23446FBEEB.html
I've included a zoom functionality similar to the one explained at this website: http://www.flashandmath.com/howtos/zoom/
Even though it does indeed work in terms of the possibility of moving the image on my stage, the drag-animation is not present, meaning there will be no "movement" of my image, just a sudden change in position.
The problem followed my attempt to change the concept from timeline-based to class-based.
Here is my Main class:
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;
public class Main extends Sprite
{
public static var scale:Number = 1;
private var _rootMC:MovieClip;
// ------------------------------
public var bg_image:Sprite;
public var spImage:Sprite;
public var mat:Matrix;
public var mcIn:MovieClip;
public var mcOut:MovieClip;
public var boardWidth:int = 980;
public var boardHeight:int = 661;
public var boardMask:Shape;
public var externalCenter:Point;
public var internalCenter:Point;
public static var scaleFactor:Number = 0.8;
public static var minScale:Number = 0.25;
public static var maxScale:Number = 10.0;
//-------------------------------
public function Main(rootMC:MovieClip)
{
_rootMC = rootMC;
bg_image = new image();
this.graphics.beginFill(0xB6DCF4);
this.graphics.drawRect(0,0,boardWidth,boardHeight);
this.graphics.endFill();
spImage = new Sprite();
this.addChild(spImage);
boardMask = new Shape();
boardMask.graphics.beginFill(0xDDDDDD);
boardMask.graphics.drawRect(0,0,boardWidth,boardHeight);
boardMask.graphics.endFill();
boardMask.x = 0;
boardMask.y = 0;
this.addChild(boardMask);
spImage.mask = boardMask;
minScale = boardWidth / bg_image.width;
mcIn = new InCursorClip();
mcOut = new OutCursorClip();
bg_image.addChild(mcIn);
bg_image.addChild(mcOut);
bg_image.scaleX = minScale;
bg_image.scaleY = minScale;
spImage.addChild(bg_image);
spImage.addChild(mcIn);
spImage.addChild(mcOut);
spImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
_rootMC.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
spImage.addEventListener(MouseEvent.CLICK, zoom);
_rootMC.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
_rootMC.stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
}
private function startDragging(mev:MouseEvent):void
{
spImage.startDrag();
}
private function stopDragging(mev:MouseEvent):void
{
spImage.stopDrag();
}
private function zoom(mev:MouseEvent):void
{
if ((!mev.shiftKey)&&(!mev.ctrlKey))
{
return;
}
if ((mev.shiftKey)&&(mev.ctrlKey))
{
return;
}
externalCenter = new Point(spImage.mouseX,spImage.mouseY);
internalCenter = new Point(bg_image.mouseX,bg_image.mouseY);
if (mev.shiftKey)
{
bg_image.scaleX = Math.max(scaleFactor*bg_image.scaleX, minScale);
bg_image.scaleY = Math.max(scaleFactor*bg_image.scaleY, minScale);
}
if (mev.ctrlKey)
{
bg_image.scaleX = Math.min(1/scaleFactor*bg_image.scaleX, maxScale);
bg_image.scaleY = Math.min(1/scaleFactor*bg_image.scaleY, maxScale);
}
mat = this.transform.matrix.clone();
MatrixTransformer.matchInternalPointWithExternal(mat,internalCenter,externalCenter);
bg_image.transform.matrix = mat;
}
private function keyHandler(ke:KeyboardEvent):void
{
mcIn.x = spImage.mouseX;
mcIn.y = spImage.mouseY;
mcOut.x = spImage.mouseX;
mcOut.y = spImage.mouseY;
mcIn.visible = ke.ctrlKey;
mcOut.visible = ke.shiftKey;
if (ke.ctrlKey || ke.shiftKey)
{
Mouse.hide();
}
else
{
Mouse.show();
}
}
}
}
Here is my image class:
package {
import fl.motion.MatrixTransformer;
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.MouseEvent;
public class image extends MovieClip
{
public function image()
{
this.width = 980
this.height = 500
this.y = 0
this.x = 0
}
}
I know this is a lot of code, but I am really stuck :(
On your MOUSE_DOWN handler, you need to start listening to the MOUSE_MOVE event.
On your MOUSE_UP handler, you need to stop listening to the MOUSE_MOVE event.
Not sure if you are already doing this.
In your MOUSE_MOVE event handler, you need to update the x / y positions of the image, in a similar way to what you are probably doing on the MOUSE_UP handler.
I am making a website in Flash, coded in flashbuilder. Whenever I try to export my code I get the same error again and again (TypeError = see below).
I think the problem has something to do with the stage of my project. Whenever I change the var stageMiddenX = (stage.stageWidth / 2); into var stageMiddenX = 512;, the code works. but I wan't the var to be dynamic.
TypeError
Error #1009: cannot access a property or method of a null object reference at main()
package {
import flash.display.MovieClip;
public class main extends MovieClip{
var stageMiddenX = (stage.stageWidth / 2);
var stageMiddenY = (stage.stageHeight / 2);
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
}
}
The issue here is that stage is not yet available at the time you are requesting it.
You'll want to wait until the Event.ADDED_TO_STAGE event is fired before attempting to acccess stage.
package {
import flash.display.MovieClip;
public class main extends MovieClip{
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
private var stageMiddenX:Number;
private var stageMiddenY:Number;
public function main(){
if(stage) init(null);
else addEventListener(Event.ADDED_TO_STAGE, init)
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stageMiddenX = (stage.stageWidth / 2);
stageMiddenY = (stage.stageHeight / 2);
}
}
}
Put the stuff accessing stage into a constructor (assuming this is your document class)..
package
{
import flash.display.MovieClip;
public class main extends MovieClip
{
public var stageMiddenX:int;
public var stageMiddenY:int;
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
public function main()
{
stageMiddenX = stage.stageWidth / 2;
stageMiddenY = stage.stageHeight / 2;
}
}
}