Starling Weird Error when Adding Child - actionscript-3

I'm Following along with Lynda Tutorial Building Flash Games with Starling
and at some point i got very Weird Error.
I've a Class Background In Package objects:
package objects
{
import core.Assets;
import starling.display.Image;
import starling.display.Sprite;
public class Background extends Sprite
{
private var sky1:Image;
private var sky2:Image;
public function Background()
{
sky1 = new Image(Assets.skyTexture);
addChild(sky1);
sky2 = new Image(Assets.skyTexture);
sky2.y = -800;
addChild(sky2);
}
public function update():void
{
sky1.y += 4;
if(sky1.y == 800)
{
sky1.y = -800;
}
sky2.y +=4;
if(sky2.y == 800)
{
sky2.y = -800;
}
}
}
}
and Menu class in Package states:
package states
{
import flash.display.Sprite;
import core.Game;
import interfaces.IState;
import objects.Background;
import starling.events.Event;
public class Menu extends Sprite implements IState
{
private var game:Game;
private var background:Background;
public function Menu(game:Game)
{
this.game = game;
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(event:Event):void
{
background = new Background();
addChild(background);
}
public function update():void
{
}
public function destroy():void
{
}
}
}
on the line
addChild(background);
I get this weird Error and I'm sure there's no any Errors in any other Class
Implicit coercion of a value of type Background to an unrelated type DisplayObject. Menu.as /Spacer/src/states line 31 Flex Problem
When i debug without this Line i get no Errors.

Your Menu class extends flash.display.Sprite but Background class extends starling.display.Sprite. The Menu class should be extended from the starling Sprite.

Related

Symbol class to document class AS3

I have this code in my document class:
package {
import flash.events.Event;
public class Main extends MovieClip {
public var mainMenu = new MainMenu();
public function Main() {
// constructor code
startGame();
}
public function startGame(){
mainMenu.x = stage.stageWidth/2
addChild(mainMenu);
}
public function initGame(event){
//Adding player with and stuff
}
}
}
And this in my MainMenu class:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class MainMenu extends MovieClip {
private var logo = new Logo();
public function MainMenu() {
// constructor code
logo.x = - logo.width/2;
logo.y = 50;
logo.addEventListener(MouseEvent.CLICK, initGame);
addChild(logo);
}
}
}
I get this message when i try to test the game; 1120: Access of undefined property initGame.
Why can't the mainMenu.as access the public function initGame?
Thanks
You require a reference to the Main object inside MainMenu. You can pass it through the constructor:
mainMenu = new MainMenu(this);
And inside MainMenu you can now register an event to a method inside Main.
public function MainMenu(main:Main) {
logo.addEventListener(MouseEvent.CLICK, main.initGame);
//...
}

as3 StageWebView in a class not displaying

I am sure this is an easy one. I have one Main.as class calling a another class that is loading a StageWebView. If called by itself the StageWebView works fine, but when I call it from another class it will not display. What simple thing am I forgetting?
Perhaps it has something to do with the "stage" in the loaded class?
Main.as
public function addPopeNews()
{
thePopeNews = new popeNews();
addChild(thePopeNews);
}
PopeNews.as
package com
{
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.*;
import flash.net.URLRequest;
import flash.media.StageWebView;
import flash.geom.Rectangle;
public class popeNews extends MovieClip
{
public var backBar:popeNewsBar;
public var webView:StageWebView;
public function popeNews()
{
webView=new StageWebView();
webView.stage = this.stage;
webView.loadURL("www.myUrl.com");
trace("POPE NEWS!!!"); /// trace works!
backBar = new popeNewsBar();
backBar.width = Main._screenX;
backBar.scaleY = backBar.scaleX;
webView.addEventListener(Event.COMPLETE, webLoaded);
webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING,onChanging);
}
public function webLoaded(e:Event)
{
trace("web loaded"); // trace works!!
if (webView.isHistoryBackEnabled)
{
addChild(backBar);
backBar.bb.addEventListener(MouseEvent.CLICK, goBack);
webView.viewPort = new Rectangle(0,backBar.height,Main._screenX,Main._screenY - backBar.height);
}
else
{
webView.viewPort = new Rectangle(0,0,Main._screenX,Main._screenY);
}
}
public function goBack(e:Event)
{
if (webView.isHistoryBackEnabled)
{
trace("Called GO BACK");
webView.historyBack();
removeChild(backBar);
backBar.bb.removeEventListener(MouseEvent.CLICK, goBack);
return;
}
if (webView.isHistoryForwardEnabled)
{
webView.historyForward();
return;
}
}
public function onError(e:ErrorEvent):void
{
//infoBox.text="Page is not available. Try reloading.";
}
public function onChanging(e:LocationChangeEvent):void
{
//webView.viewPort = null;
trace("Called CHANGING!!!");
}
///
}
}
You are right, the stage is null in the PopeNews constructor. You should put your initialization code into a new method, and listen for the ADDED_TO_STAGE event.
public function popeNews()
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(ev:Event):void
{
webView=new StageWebView();
webView.stage = this.stage;
webView.loadURL("www.myUrl.com");
trace("POPE NEWS!!!"); /// trace works!
backBar = new popeNewsBar();
backBar.width = Main._screenX;
backBar.scaleY = backBar.scaleX;
webView.addEventListener(Event.COMPLETE, webLoaded);
webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING,onChanging);
}
Also, by convention class names are capitalized.

Access of undefined property through static type

I got an error : access of possibly undefined property destinationY through a reference with static type Gold. I don't use the destinationY property in Gold class. Only in main class.
I'm still amateur at classes and defining properties for a object itself. Please help. Thanks!
Here is the Main class:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class Main extends MovieClip
{
private var field:Array;
public var gold:Gold;
public var goldContainer:Sprite = new Sprite();
private var goldTimer:Timer = new Timer(2000);
public function Main():void
{
setupField();
goldSet();
addEventListener(Event.ENTER_FRAME,onEnterFrm);
}
private function goldSet():void
{ addChild(goldContainer);
goldTimer.start();
goldTimer.addEventListener(TimerEvent.TIMER, newGold);
}
private function newGold(e:TimerEvent):void {
var goldRow:int=Math.floor(Math.random()*5);
var goldCol:int=Math.floor(Math.random()*9);
gold = new Gold(this);
gold.buttonMode=true;
goldContainer.addChild(gold);
gold.x=30+goldCol*65;
gold.destinationY = 35+goldRow*75;
gold.y=-2
}
private function onEnterFrm(e:Event):void {
for (var i:uint=0; i<goldContainer.numChildren; i++) {
var fallingGold:Gold=goldContainer.getChildAt(i) as Gold;
if (fallingGold.y<fallingGold.destinationY) {
fallingGold.y++;
} else {
fallingGold.alpha-=0.01;
if (fallingGold.alpha<0) {
goldContainer.removeChild(fallingGold);
}
}
}
}
}
}
Here is the Gold class :
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Sprite
import flash.events.MouseEvent
public class Gold extends MovieClip
{
private var _main:Main
private var gold:Gold;
public function Gold(main:Main):void
{
_main=main;
addEventListener(MouseEvent.CLICK, goldClicked);
}
private function goldClicked(e:MouseEvent):void
{
e.currentTarget.removeEventListener(MouseEvent.CLICK,goldClicked);
_main.goldContainer.removeChild(e.currentTarget as Gold);
}
}
}
From first glance, you don't have a destinationY property in your Gold Class. MovieClip does not have this property either, so you're trying to manipulate a variable that doesn't exist.
Unless this isn't the whole Class you're showing us, of course.
To elaborate a bit
I don't use the destinationY property in Gold class. Only in main
class.
Yes, but the you're trying to manipulate a property called destinationY in your Gold Class from the main class.
If you do this:
public class Gold extends MovieClip
{
private var _main:Main
private var gold:Gold;
public var destinationY:int = 0;
...
}
you shouldn't get this error anymore, but i doubt this will solve all your problems.
On line xx of the Main class, you have the code:
gold.destinationY = 35+goldRow*75;
You're trying to access the "destinationY" property of the Gold class, but the Gold class doesn't have a public property named destinationY. To add it, just add this line to the Gold class:
public var destinationY;

AS3 Object appears onClick where the mouse clicks on the stage

I want in actionscript to place an object in my library onto the stage where I clicked. Seems easy? Right? TOTALLY BLANKING. any help would be awesome :)
my code thus far is:
package code {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var redBox: Box = new Box(mouseX, mouseY);
public function Main() {
// constructor code
stage.addEventListener(MouseEvent.CLICK, mouseClickEvent);
}
public function mouseClickEvent(e:MouseEvent):void {
addChild(redBox);
}
}
}
that is the main and then the box code is:
package code {
import flash.display.MovieClip;
public class Box extends MovieClip{
public function Box(myX:Number, myY:Number) {
// constructor code
myX = x;
myY = y;
}
}
}
Just do this :
package code {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var redBox: Box = new Box();
public function Main() {
// constructor code
stage.addEventListener(MouseEvent.CLICK, mouseClickEvent);
}
public function mouseClickEvent(e:MouseEvent):void {
redBox.x = stage.mouseX;
redBox.y = stage.mouseY;
addChild(redBox);
}
}
}

Actionscript: add a sprite from a subclass to the display list of its super class?

Say i have these two classes:
MAIN.as
package
{
import flash.display.*; import mx.core.*;
import flash.events.*; import mx.collections.*;
import flash.geom.*; import mx.controls.*;
import flash.text.*; import mx.events.*;
import mx.styles.*;
import mx.containers.*;
public class MAIN extends Sprite
{
public var APPLICATION:Application = Application(Application.application);
public var keyDownText:TextField = new TextField();
public function MAIN()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
addEventListener(Event.ENTER_FRAME,STEP);
new OBJECT_square().CREATE(10,100,1);
}
public function STEP():void {}
public function DRAW():void {}
public function KEY_DOWN(event:KeyboardEvent):void
{
keyDownText.text = "Key code: " + event.keyCode;
this.addChild(keyDownText);
}
}
}
OBJECT_square.as
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_square extends Sprite
{
public var X:int = 0;
public var Y:int = 0;
public var DEPTH:int = 0;
public var SPRITE:Sprite = new Sprite();
public function CREATE(X:int,Y:int,DEPTH:int):void
{
this.DEPTH = DEPTH;
this.X = X;
this.Y = Y;
DRAW();
}
public function DRAW():void
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
addChild(SPRITE);
}
}
}
Now how is it that I can add the variable SPRITE which is a Sprite in the OBJECT_square class to the display list of MAIN class? I've tried addChild(SPRITE) and super.addChild(SPRITE). If everything works I should see a red square somewhere on the screen but right now its all blank, except for the text drawn in the MAIN class.
Basically I want it so i can just make a new OBJECT_square and it will draw itself without any more instructions from the MAIN class.
Try this:
var obj:OBJECT_square = new OBJECT_square();
obj.CREATE(10,100,1);
addChild(obj);
Or, if you really want to do it in one go, you could try this:
In main
addChild((new OBJECT_square()).CREATE(10,100,1));
And change your draw function to return the square object
public function DRAW():OBJECT_square
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
addChild(SPRITE);
return this;
}