I have a function, sceneLoader, that will instantiate an object of the Class Scene1 from a string using getDefinitionByName. I then want to access a function (sceneHandler) in Scene1, from another function.
public class Main extends MovieClip{
private var sceneNumber:int = 1;
private var SceneRef:Class;
private var sceneString:String;
public function Main(){
sceneLoader();
responseHandler();
}
private function sceneLoader():void{
sceneString = "Scene" + int(sceneNumber);
SceneRef = getDefinitionByName(sceneString) as Class;
var scene = new SceneRef();
addChild(scene);
}
private function responseHandler():void{
scene.sceneHandler(); //Obviously this will not work
}
}
Class Scene1
public class Scene1 extends MovieClip{
public function sceneHandler():void{
//Do something
}
}
The problem is that scene is out of scope. I cannot instantiate Scene1 outside the function as I need to first call getDefinitionByName. I also want to keep the function for loading scenes later on.
How can I call sceneHandler from responseHandler?
EDIT: I've tried
public class Main extends MovieClip{
private var sceneNumber:int = 1;
private var SceneRef:Class;
private var sceneString:String;
public function Main(){
sceneLoader();
responseHandler();
}
private function sceneLoader():void{
addChild(getScene());//Does not work
}
private function responseHandler():void{
getScene().sceneHandler(); //Works now
}
private function getScene():Object{
sceneString = "Scene" + int(sceneNumber);
SceneRef = getDefinitionByName(sceneString) as Class;
var scene = new SceneRef();
//addChild(getScene());//This does work but doesn't suit my need
return scene;
}
}
Now I can't add the object to the stage.
I get the Error:
Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.
I figured it out. I just forgot to cast as a DisplayObject.
public class Main extends MovieClip{
private var sceneNumber:int = 1;
private var SceneRef:Class;
private var sceneString:String;
public function Main(){
sceneLoader();
responseHandler();
}
private function sceneLoader():void{
addChild(getScene() as DisplayObject);
}
private function responseHandler():void{
getScene().sceneHandler();
}
private function getScene():Object{
sceneString = "Scene" + int(sceneNumber);
SceneRef = getDefinitionByName(sceneString) as Class;
var scene = new SceneRef();
return scene;
}
}
Obviously:
import flash.display.DisplayObject;
Related
I have no idea what is causing the error. I'm sure its very obvious.
(See comments below).
package src.main{
import src.tilespack.Tile;
public class Main{
public var tile:Tile;
public function Main{
var t:Tile = Tile.tiles[0]; //Error here
trace(Tile); //This will also cause an error
}
Tile:
package src.tilespack{
public static var tiles:Array = [];
public static var floorTile:Tile = new FloorTile(0); //Causes an error in FloorTile Class
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
FloorTile:
package src.tilespack{
import src.gfx.Assets;
import src.tilespack.Tile;
public class FloorTile extends Tile{ //Error here
public function FloorTile(ID:int){
super(Assets.floorTileData, ID);
}
}
}
Error #1009: Cannot access a property or method of a null object
reference.
I've noticed few problems in your code:
1) The file Tile doesn't have a definition of class. Probably, this is just a typo, but anyway, this file should look something like:
package src.tilespack
{
public class Tile
{
public static var tiles:Array = [];
public static var floorTile:Tile = new FloorTile(0); //Causes an error in FloorTile Class
public var ID:int;
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
}
2) Your code has something like "recursive linkage" (sorry, I don't know the official term). Your class Tile has the static variable floorTile, and it tries to create an instance of the FloorTile class, which itself extends the class Tile.
So we have a situation, when the class Tile tries to use the class FloorTile (because static variables should be instantiated during the first class use), and the class FloorTile tries to use the class Tile. And it's a problem.
You may either remove static variable of the type FloorTile or change code of the class Tile to prevent usage of the class FloorTile before the class Tile is prepared to work. Here an example of the second approach:
package src.tilespack
{
import flash.display.BitmapData;
public class Tile
{
public static var tiles:Array = [];
private static var _floorTile:Tile;
public static function get floorTile():Tile
{
if (!Tile._floorTile)
{
Tile._floorTile = new FloorTile(0);
}
return Tile._floorTile;
}
public var ID:int;
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
}
I have an instance of Main that I should be able to access anywhere.
If I want to access a variable on level I should be able do:
_root.level.my_value
However .level is showing up null when I call _root.level in my map.
Main.as (Class Document)
package{
import flash.display.MovieClip;
public class Main extends MovieClip{
//Monostate
private static var _instance:Main;
public static function get instance():Main { return _instance; }
public var level:MovieClip;
public function Main(){
_instance = this; //Monostate
this.level = new Level();
}
}} //package / class
Level.as
package{
import flash.display.MovieClip;
public class Map extends MovieClip{
private var _root:MovieClip;
public function Map(){
_root = Main.instance
trace(_root);
trace(_root.level); //This should not be null
}
}} //package / class
You are never instantiating your _instance, so level never gets set.
Your static getter should probably be something more like this:
public static get instance():Main {
if(!_instance){
_instance = new Main();
}
return _instance;
}
Well, I have a problem, with searching changes of array in ItemProperty class. When i calling to trace item array length, than it gets 0. But in ItemProperty class only in RecieveIndex function item length getting more than 2. Even ReturnData() got 0. The question is how to get final result of this array(Item) in InventoryData class?
That is class, when i got 0:
public class InventoryData extends Sprite {
private var item:ItemProperty = new ItemProperty();
public function InventoryData(){
trace(item.Item);
trace(item.Item.length);
//trace(item.ReturnData());
}
}
Class where array was formed. In RecieveIndex method Item length more than 0.
public class ItemProperty extends Sprite{
public var Item:Array = [];
public function ItemProperty() {
var connect:Connection = new Connection();
connect.setRequest("Select", "`inventory`", ["all"], ["all"], InventoryContent);
}
private function InventoryContent(RecieveData:Array):void
{
var picking:Connection = new Connection();
picking.setRequest("Select","`weapon`",["all"],["id IN("+RecieveData[0].contents.toString() + ")"], InventotyData);
}
private function InventotyData(RecieveData:Array):void{
var indexdecode:Connection = new Connection();
for(var i:uint=0; i<RecieveData.length; i++)
{
indexdecode.loadImageFromBase64(RecieveData[i].id, RecieveIndex);
}
}
public function RecieveIndex(index:int):void {Item.push(index); }
public function ReturnData():Array {return Item;}
}
Something like this with the event I mentioned in my comment:
public class InventoryData extends Sprite {
private var item:ItemProperty = new ItemProperty();
public function InventoryData(){
item.addEventListener(ItemProperty.ARRAY_READY, onArrayReady);
}
private function onArrayReady(e:Event):void {
trace(item.Item.length);
}
}
In ItemProperty class you declare a new constant:
public static const ARRAY_READY:String = "arrayReady";
and once you have the array filled with the items you want to retrieve, you add:
dispatchEvent(new Event(ARRAY_READY));
I keep getting the TypeError: Error #1010 when trying to create the constructor code for a small game in AS3. The code that appears to be causing the issue is:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
var screen1:StartScreen;
var screen2:InstructionsScreen;
var screen3:SelectScreen;
var screen4:Game1Screen;
var screen5:Game2Screen;
var screen6:Game3Screen;
var screen7:FailScreen;
var screen8:CompleteScreen;
public function Main(){
screen1 = new StartScreen();
screen2 = new InstructionsScreen();
screen3 = new SelectScreen();
screen4 = new Game1Screen();
screen5 = new Game2Screen();
screen6 = new Game3Screen();
screen7 = new FailScreen();
screen8 = new CompleteScreen();
screen1.startBtn.addEventListener(MouseEvent.CLICK,gotoSelect);
screen1.instBtn.addEventListener(MouseEvent.CLICK,gotoInst);
screen2.startBtn.addEventListener(MouseEvent.CLICK,gotoSelect2);
screen3.game1Btn.addEventListener(MouseEvent.CLICK,gotoGame1);
screen3.game2Btn.addEventListener(MouseEvent.CLICK,gotoGame2);
screen3.game3Btn.addEventListener(MouseEvent.CLICK,gotoGame3);
screen4.failBtn.addEventListener(MouseEvent.CLICK,gotoFail1);
screen4.winBtn.addEventListener(MouseEvent.CLICK,gotoWin1);
screen5.failBtn.addEventListener(MouseEvent.CLICK,gotoFail2);
screen5.winBtn.addEventListener(MouseEvent.CLICK,gotoWin2);
screen6.failBtn.addEventListener(MouseEvent.CLICK,gotoFail3);
screen6.winBtn.addEventListener(MouseEvent.CLICK,gotoWin3);
addChild(screen1);
}
private function gotoSelect(evt:MouseEvent):void{
removeChild(screen1);
addChild(screen3);
}
private function gotoInst(evt:MouseEvent):void{
removeChild(screen1);
addChild(screen2);
}
private function gotoSelect2(evt:MouseEvent):void{
removeChild(screen2);
addChild(screen3);
}
private function gotoGame1(evt:MouseEvent):void{
removeChild(screen3);
addChild(screen4);
}
private function gotoGame2(evt:MouseEvent):void{
removeChild(screen3);
addChild(screen5);
}
private function gotoGame3(evt:MouseEvent):void{
removeChild(screen3);
addChild(screen6);
}
private function gotoFail1(evt:MouseEvent):void{
removeChild(screen4);
addChild(screen7);
}
private function gotoWin1(evt:MouseEvent):void{
removeChild(screen4);
addChild(screen8);
}
private function gotoFail2(evt:MouseEvent):void{
removeChild(screen5);
addChild(screen7);
}
private function gotoWin2(evt:MouseEvent):void{
removeChild(screen5);
addChild(screen8);
}
private function gotoFail3(evt:MouseEvent):void{
removeChild(screen6);
addChild(screen7);
}
private function gotoWin3(evt:MouseEvent):void{
removeChild(screen6);
addChild(screen8);
}
}
}
And the error message that appears when I try and run this is:
TypeError: Error #1010: A term is undefined and has no properties.
at Main()
I don't see anything wrong with the code itself. I'm guessing that one of the movieclips screen1 to screen6 don't have the named buttons defined, or the StartScreen etc classes don't exist or are accessible.
I am making a basic point n' click game and I came upon this error:
TypeError: Error #1034: Type Coercion failed: cannot convert 3 to cem.mouvement.
Here's my script:
package cem {
import flash.events.Event;
import flash.display.MovieClip;
import cem.microjeux.events.InfoJeuEvent;
import cem.mouvement;
import flash.events.MouseEvent;
public class monterJeu extends MovieClip
{
private static var pType:String = "type";
private static var pNom:String = "testNom";
private static var pCourriel:String = "test#hotmail.com";
private static var pDifficulte:int = 0;
private static var pLangue:int = 0;
private static var pTitre:String = "Veuillez sortir";
private static var pVersion:String = "1.5";
private static var pCoordonnees:Number;
private var environnementJeu:environnement = new environnement();
private var personnageJeu:personnage = new personnage();
public function monterJeu():void
{
jouer(pNom,pDifficulte,pLangue);
dispatchEvent(new InfoJeuEvent(pType,pNom,pCourriel,pTitre,pVersion));
stage.addEventListener(MouseEvent.CLICK, test);
}
public function jouer(PNom:String,PDifficulte:int,PLangue:int):void
{
addChild(environnementJeu);
addChild(personnageJeu);
}
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
mouvement(3);
}
}
}
And on mouvement();
package cem
{
public class mouvement {
public function mouvement(blabla) {
trace(blabla);
}
}
}
I searched everywhere I could, and didn't find anything. I have no instances on the stage. Everything is imported on the first frame. I am kind of a beginner (let's say i'm no good at programming), so you can notify at the same time if you something that needs to be corrected. (BTW, the strange words are in french ;D)
Thanks!
The error is due to you trying to cast 3 to mouvement.
I think what you want is something like
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
var mouve:mouvement = new mouvement(3);
}
Notice that you have to have new in order to create a new instance of a class.
On another note, you should capitilize classes so they stand out better. So I would name the class Mouvement.
You are trying to cast 3 to the class mouvement into the test function:
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
new mouvement().mouvement(3); // <-- here your error
}
If you have only a function into your class you don't need to create a class but you can put on the function alone:
package cem
{
public function mouvement(blabla):void {
trace(blabla);
}
}
and now you can call the mpuvement function normally into you test function:
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
mouvement(3);
}