Search code examples
actionscript-3flash-builderstarling-framework

Starling Weird Error when Adding Child


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.


Solution

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