Adobe Animate Starling Error - actionscript-3

I have created hungryherogame (recreate) in adobe animate using starling framework..
download from this address below :
( http://www.test.farsvila.ir/animate_starling_Error.rar )
package {
import flash.display3D.textures.Texture;
import flash.utils.Dictionary;
import flash.display.Bitmap;
import starling.textures.Texture;
import starling.display.Sprite;
import starling.events.Event;
import starling.display.Image;
import starling.display.Button;
import starling.core.Starling;
import starling.events.Event;
import screens.Welcome;
public class Assets {
[Embed(source = "media/graphics/bgWelcome.jpg")]
public static const BgWelcome: Class;
[Embed(source = "media/graphics/welcome_hero.png")]
public static const WelcomeHero: Class;
[Embed(source = "media/graphics/welcome_title.png")]
public static const WelcomeTitle: Class;
[Embed(source = "media/graphics/welcome_playButton.png")]
public static const WelcomePlayBtn: Class;
[Embed(source = "media/graphics/welcome_aboutButton.png")]
public static const WelcomeAboutBtn: Class;
private static var gameTextures: Dictionary = new Dictionary();
public static function getTexture(name: String): Texture {
if (gameTextures[name] == undefined) {
var bitmap: Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitmap(bitmap);
}
return gameTextures[name];
}
}
}
Its made an error --> Call to a possibly undefined method fromBitmap through a reference with static type Class.

in first look, every thing is OK! according DOC fromBitmap is a public static member of starling.textures.Texture
but problem is because of import flash.display3D.textures.Texture which is out of code block in your post and make me hanged some minutes !
so in this case, we have same class names, two Textures. compiler get mixed up too (Ambiguous reference Error).
try it
Edited
public static function getTexture(name: String): starling.textures.Texture {
if (gameTextures[name] == undefined) {
var bitmap: Bitmap = new Assets[name]();
gameTextures[name] = starling.textures.Texture.fromBitmap(bitmap);
}
return gameTextures[name];
}
to make it clear for compiler, which Texture is your mean
Suggestion
i guess you don't really need import flash.display3D.textures.Texture;
so remove it from your default code (problem solved without changing Texture to starling.textures.Texture)

Related

Starling cannot access a property or method of a null object reference

Hey any help appreciated i've been mind boggled by the asset manager for a couple of days
Here is my Hawaii class that I'm trying to reference assets from my asset manager class (MapAssetLoadQeue). and I'm getting the Cannot access a property or method of a null object reference. Error, the error is thrown in my Hawaii class if I don't (Null) the Asset Manager reference, and it is thrown in my Loader Class haw.showHawaii()
Thanks!!!
package Maps
{
import flash.filesystem.File;
import Enqueue.MapAssetLoadQeue;
import starling.animation.IAnimatable;
import starling.animation.Juggler;
import starling.core.Starling;
import starling.display.Image;
import starling.display.MovieClip;
import starling.display.Sprite;
import starling.events.Event;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import starling.utils.AssetManager;
public class Hawaii extends Sprite
{
public var mal:MapAssetLoadQeue = new MapAssetLoadQeue;
public var Ocean:Image;
public function Hawaii():void{
super();
this.addEventListener
(starling.events.Event.ADDED_TO_STAGE, showHawaii);
}
public function showHawaii(e:Event):void{
if(mal.mapAssets != null) {
Ocean = new Image(mal.mapAssets.getTexture("blue"));
addChild(Ocean);
Ocean.x = 0;
Ocean.y = 0;
}
}
Here is my loader class where I load my assets, and here is where I get my error. haw.showHawaii(null) function. Cannot access a property or method of a null object reference.
package Assets {
import flash.filesystem.File;
import Enqueue.MapAssetLoadQeue;
import GUI.Wave;
import Maps.Hawaii;
import starling.animation.IAnimatable;
import starling.animation.Juggler;
import starling.core.Starling;
import starling.core.starling_internal;
import starling.display.Image;
import starling.display.MovieClip;
import starling.display.Sprite;
import starling.events.Event;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import starling.utils.AssetManager;
public class Loader extends Sprite {
var loadText: Texture = Texture.fromEmbeddedAsset(loadMCTexture);
var loadXml: XML = XML(new loadMC0Xml());
var loaderAtlas: TextureAtlas = new TextureAtlas(loadText, loadXml);
public var loaderMovie: MovieClip = new MovieClip(loaderAtlas.getTextures("loading_"));
public var mal: MapAssetLoadQeue = new MapAssetLoadQeue;
public var wave:Wave = new Wave;
public var haw:Hawaii;
//Loader MovieClip size 720X233;
[Embed(source = "/../Wavehunter/loadScreenAssets/wm/MovieClips/loader/loader_0.xml", mimeType = "application/octet-stream")]
public static const loadMC0Xml: Class;
[Embed(source = "/../Wavehunter/loadScreenAssets/wm/MovieClips/loader/loader_0.png")]
public static const loadMCTexture: Class;
// public var juggler:Juggler = Starling.juggler;
/*
======================================================================
Every Movieclip asset for the worldMap
> Load WorldMap Assets on stage as Png and Make all alpha to 30%
> and within each function make that png invible to false or remove
> add MoviClip over the invisible png after load is processed
=======================================================================
*/
// Load WorldMap asset function
public function mapLoader(): void {
mal.requestWM();
addChild(loaderMovie);
loaderMovie.x = 347;
loaderMovie.y = 277;
loaderMovie.play();
Starling.juggler.add(loaderMovie);
}
public function mapLdr(){
loaderMovie.visible = true;
addChild(loaderMovie);
loaderMovie.x = 347;
loaderMovie.y = 277;
loaderMovie.play();
Starling.juggler.add(loaderMovie);
}
//this function resizes the loader to original size and places at the bottom corner
public function resizeLdr(){
loaderMovie.width = 72;
loaderMovie.height = 22;
loaderMovie.x =1237;
loaderMovie.y = 712;
}
public function callLoad(): void {
public function callHawaii(): void {
trace(" Hawaii is being runned");
//mal.mapAssets.verbose = true;
mal.mapAssets.loadQueue(function onGo(ratio: Number): void {
trace("Hawaii percentage is", ratio);
if (ratio == 1.0) {
Starling.juggler.delayCall(function (): void {
loaderMovie.visible = false;
//addChild(haw.Ocean);
// addChild(haw);
trace("this is haw value",haw);
//trace("this is the function", haw.showHawaii(null));
haw.showHawaii(null);
}, 0.30);
}
});
}
public function showMap() {
// get texture by name
addChild(mal);
mal.accesMap();
mal.hawaii.addEventListener(Event.TRIGGERED,removeMap);
}
// this code removes the HawaiiMap from the screen and goes directly to the wave
public function removeMap(){
trace("function initated");
mapLdr();
resizeLdr();
mal.mapAssets.purge();
mal.requestHawaii();
callHawaii();
mal.hawaii.removeEventListener(Event.TRIGGERED,removeMap);
}
public function Loader() {
super();
}
}
}
And here is my Asset Manager class
package Enqueue {
import flash.filesystem.File;
import Assets.MapAnimate;
import Assets.MapAssets;
import Maps.Hawaii;
import starling.animation.IAnimatable;
import starling.animation.Juggler;
import starling.animation.Transitions;
import starling.animation.Tween;
import starling.core.Starling;
import starling.core.starling_internal;
import starling.display.Button;
import starling.display.Image;
import starling.display.MovieClip;
import starling.display.Sprite;
import starling.events.EnterFrameEvent;
import starling.events.Event;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import starling.utils.AssetManager;
public class MapAssetLoadQeue extends Sprite {
public var mapAssets: AssetManager;
public function MapAssetLoadQeue() {
super();
}
public function requestHawaii():void {
// mapAssets.verbose =false;
// mapAssets = new AssetManager;
var appDir:File = File.applicationDirectory;
mapAssets.enqueue(appDir.resolvePath("IslandMaps/Hawaii"));
//mapAssets.enqueue(Hawaii);
mapAssets.verbose = true;
}
If this is all of your code, and if I'm not missing something obvious, I don't see you creating "haw" anywhere. That is, you don't appear to have anything such as...
haw = new Hawaii();
...so, it would be null!

AS3: ReferenceError: Error #1069:

I'm trying to load a sound file into my Flash project. I keep getting this error however.
ReferenceError:
Error #1069: Property COMPLETE not found on flash.events.Event and there is no default value. at LoadSND/soundLoaded()[C:\Users\Admin\Desktop\Final Project\LoadSND.as:38]
The relevant code:
package {
import flash.events.*;
import flash.media.*;
import flash.net.URLRequest;
public class LoadSND {
//declare variables
private var sndTrack: Sound;
private var sndChannel: SoundChannel;
private var sndVolume: Number;
private var newTrack: String;
private var canRepeat: Boolean;
public function LoadSND(myTrack: String, myRepeat: Boolean = true) {
// constructor code
// set a default volume and track
sndVolume = 0.5;
setTrackData(myTrack, myRepeat);
}
private function loadSound(): void {
// first stop all old sounds playing
SoundMixer.stopAll();
// create a new sound for the track and a new sound channel
sndTrack = new Sound();
sndChannel = new SoundChannel();
// load the required sound
sndTrack.load(new URLRequest(newTrack));
// when loaded – play it;
sndTrack.addEventListener(Event.COMPLETE, soundLoaded);
}
private function soundLoaded(Event): void {
// finished with this listener so remove it
sndTrack.removeEventListener(Event.COMPLETE, soundLoaded);
// call the play sound function
playSound();
}
private function playSound(): void {
// assign music to the musicChannel and play it
sndChannel = sndTrack.play();
// setting the volume control property to the sound channel
sndChannel.soundTransform = new SoundTransform(sndVolume, 0);
// but add this one to make repeats
sndChannel.addEventListener(Event.SOUND_COMPLETE, playAgain);
}
private function playAgain(Event): void {
// remove this listener and repeat playSound()
sndChannel.removeEventListener(Event.SOUND_COMPLETE, playAgain);
playSound();
}
private function setTrackData(myTrack: String, myRepeat: Boolean): void {
// update the new track information
newTrack = myTrack;
canRepeat = myRepeat;
// and load it
loadSound();
}
private function setVolumeLevel(Number): void {
}
} //end class
} //end package
Loading a default track through my Main.as
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.display.SimpleButton;
import flash.utils.Dictionary;
import flash.text.TextFormat;
import flash.net.*;
import flash.events.*;
import fl.controls.*;
import flash.media.*;
import fl.events.ComponentEvent;
import fl.managers.StyleManager;
import fl.data.DataProvider;
import fl.data.SimpleCollectionItem;
import fl.managers.StyleManager;
import fl.events.ComponentEvent;
import flash.events.Event;
import flash.net.SharedObject;
import LoadSWF;
import GameButton;
import LoadSND;
public class Main extends MovieClip {
//Sound Variables
private var MAX_TRAX: int = 7;
private var MAX_SFX: int = 9;
private var sndPath: String;
private var sndTrack: LoadSND;
private var isMuted: Boolean;
private var canRepeat: Boolean;
private var sndVolume: Number;
public function Main() {
// constructor code
sndPath = "musicSFX/Fury.mp3";
isMuted = false;
sndTrack = new LoadSND(sndPath, canRepeat);
}
Any help is appreciated :) Thanks
The problem lies in the definition of soundLoaded function. You put just class in there instead of argument with type declaration. It should be solved if you adjust definition of the soundLoaded function in the following way:
private function soundLoaded(event:Event): void
By the way same problem is in functions playAgain and setVolumeLevel.

Where must be custom logic in pureMVC (as3)?

I tried to write small as3 program based on framework pureMVC.
I understood basic principles of it, but I can't understand, where I must place custom logic.
For example, I must load 10 images. I created command, that init Proxy.
package app.controller
{
import app.model.GalleryProxy;
import dicts.Constants;
import org.puremvc.interfaces.INotification;
public class LoadFilesCommand extends BaseCommand
{
public function LoadFilesCommand() { }
override public function execute(note:INotification):void
{
facade.registerProxy(new GalleryProxy(Constants.FILES_LIST));
}
}
}
And Proxy is:
package app.model
{
import dicts.Constants;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import org.puremvc.interfaces.IProxy;
import org.puremvc.patterns.proxy.Proxy;
public class GalleryProxy extends Proxy implements IProxy
{
public function GalleryProxy(list:Vector.<String>)
{
super(Constants.PROXY_GALLERY);
_fileList = list;
_total = _fileList.length;
load();
}
public function get currentImage():Bitmap
{
return _images[_index];
}
//--------------------------------------------------------------------------
// PRIVATE SECTION
//--------------------------------------------------------------------------
private var _fileList:Vector.<String>;
private var _total:uint;
private var _loaded:uint = 0;
private var _images:Array = [];
private var _index:int;
private function load():void
{
var loader:Loader;
for (var i:int = 0; i < _total; i++)
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.load(new URLRequest(_fileList[i]));
}
}
private function imageLoadHandler(event:Event):void
{
var info:LoaderInfo = LoaderInfo(event.currentTarget);
_images[Constants.FILES_LIST.indexOf(info.url)] = info.content;
info.removeEventListener(Event.COMPLETE, imageLoadHandler);
info.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
_loaded++;
if (_loaded >= _total)
sendNotification(Constants.COMMAND_SHOW_MAIN);
}
private function errorHandler(event:ErrorEvent):void
{
throw new Error("bad link or internet disconnect");
}
}
}
Now my Proxy is loading images independently (functions load() and imageLoadHandler)
Is it correct?
Or I must move this logic to Command class?
Or I must create some LoadService.as, which will contains this logic?
What is the correct variant for pureMVC?
Do you want to load your 10 images on application startup? If not, make load() public and call it from a Mediator, responding to a UI event.
If so, what you have will work fine. One alternative would be writing GalleryProxy so it doesn't call load() in the constructor - instead, you could have the Command register the proxy, load the image list, and call proxy.load(images[i]) in a loop.

AS3 I don't understand the different treatment of an extended movieclip class vs extended simplebutton class

I recently discovered the custom classes in actionscript 3. I started using them in my latest project but I find it hard to bend my brain around how it all works.
I created two different classes to test.
One is an extended movieclip called "Persoon" and the other is an extended simplebutton called "SpeakerBtn".
package {
import flash.display.Sprite;
public class Persoon extends Sprite {
public function Persoon(xPos:Number, yPos:Number, naam:String) {
var persoon:Sprite = new Sprite;
persoon.graphics.beginFill(0x000000,1);
persoon.graphics.drawCircle(xPos, yPos, 2);
persoon.graphics.endFill();
this.addChild(persoon);
trace ("hij heet " + naam);
}
}
}
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
public class SpeakerBtn extends SimpleButton {
public var snd:Sound;
public var cnl:SoundChannel = new SoundChannel();
public function SpeakerBtn(xp:Number,yp:Number,naam:String) {
var speaker:SimpleButton = new SimpleButton();
speaker.name = naam;
speaker.x = xp;
speaker.y = yp;
speaker.addEventListener(MouseEvent.CLICK, playSnd);
//this.addChild(speaker);
}
public function playSnd (event:MouseEvent) : void {
trace ("ping");
}
}
}
Then I have my main:
package {
import flash.display.MovieClip;
import SpeakerBtn;
import flash.display.SimpleButton;
import Persoon;
public class Main extends MovieClip {
var sp:SpeakerBtn;
var ps:Persoon;
public function Main() {
sp = new SpeakerBtn(50,50,"donna");
addChild(sp);
ps = new Persoon(300,300,"wilf");
addChild(ps);
}
}
}
Persoon wilf works like I expected, displays fine and traces correctly.
SpeakerBtn donna does not display and does not trace correctly. I commented out the addChild in the SpeakerBtn package because if I turn it on, I get the error 1061: Call to a possibly undefined method addChild through a reference with static type SpeakerBtn
I noticed that when I define the x and the y and addChild in Main for the speakerBtn it does work. But I don't want to have to define all that in Main, I want my SpeakerBtn to do all that.
I checked this question but it does not provide me with an answer. Can someone explain to me what is happening, or alternatively link me to a comprehensible tutorial (one not too heavy on techspeak, more like an explain-it-to-me-like-I'm-5-years-old)? Thanks!
Update
I forgot to add a button with the class SpeakerBtn to my library, so there was nothing to display. Fixed that now, and with this code the button does appear on the stage, only the x and y values are not registered and it appears on 0,0. Also, the event playSnd does not trigger the trace and I assume is not working.
Solution
With help of Cherniv's information I came to the following solution for my SpeakerBtn.
Main does this:
package {
import flash.display.MovieClip;
import SpeakerBtn;
import flash.display.SimpleButton;
public class Main extends MovieClip {
var sp:SpeakerBtn;
public function Main() {
sp = new SpeakerBtn("donna", 300, 50);
addChild(sp);
}
}
}
And SpeakerBtn does this:
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class SpeakerBtn extends SimpleButton {
private var snd:Sound;
private var cnl:SoundChannel = new SoundChannel();
private var _naam:String;
private var _x:Number;
private var _y:Number;
public function SpeakerBtn(naam:String, xp:Number, yp:Number) {
_naam = naam;
_x = xp;
_y = yp;
addEventListener(Event.ADDED_TO_STAGE, addBtn);
}
private function addBtn (event:Event) : void {
this.x = _x;
this.y = _y;
this.name = _naam;
snd = new Sound(new URLRequest("mp3/" + _naam + ".mp3"));
addEventListener(MouseEvent.CLICK, playSnd);
}
private function playSnd (event:MouseEvent) : void {
cnl = snd.play();
}
}
}
So what I did was add an EventListener for when the button was added to the stage and then set all the variables like x-position, y-position and name.
That's because of inheritance. Your SpeakerBtn doesn't inherits the addChild method from his ancestors , because as we can see in SimpleButton's documentation it is inheritor of DisplayObject and not of DisplayObjectContainer , which do have a addChild method and passes it to all his inheritors including MovieClip and Persoon.

AS 3.0 reference problem

I am finding it hard to fugure out the reference system in AS 3.0.
this is the code i have (i have trimmed it down in order to find the problem but to no avail)
package rpflash.ui {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;
import nowplaying;
import flash.text.TextField;
public class RPUserInterface extends Sprite{
var np:nowplaying;
public function RPUserInterface(){
}
public function init(){
var np:nowplaying = new nowplaying();
this.addChild(np)
}
public function updateplayer(xml:XML){
var artist: String = xml.nowplaying.artist.toString();
var title: String = xml.nowplaying.title.toString();
trace("UI:update");
trace(this.np);// this gives me a null reference
}
}
}
and still i cannot access np!!! trace this.np gives me a null reference. i am not even trying to access it from a subling class. (btw i def want to know how to do that as well.)
In your init() function, you are instantiating a local variable called np. Try this instead:
public function init() {
// var np:nowplaying = new nowplaying();
np = new nowplaying();
this.addChild(np);
}
Also, make sure init() is getting called before updateplayer(). Hope that helps.