AS 3.0 reference problem - actionscript-3

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.

Related

Adobe Animate Starling Error

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)

Action Script 3 (1136:Incorrect Number of arguments)

I am starting with as3, but i find some error in program popping up.
My as3 program:
var a:Array=new Array();
var i:int;
for(i=1;i<=3;i++){
var k:Hello = new Hello(i);
a.push(k);
}
for(i=0;i<a.length;i++){
a[i].x=i*100;
trace(a[i].age);
addChild(a[i]);
}
and my as program :
package{
import flash.display.MovieClip;
import flash.display.stage;
public class Hello extends MovieClip{
public var age:int;
public function Hello(i:int){
this.age=m;
}
}
}
You assigned m variable to age. What is m? You should write this.age=i;.
public function Hello(i:int){
this.age=m;
}
And remove import flash.display.stage;.

Passing a variable between classes

I am trying to pass a variable "budget" from my DocumentClass of a flash file, to a class.
Currently I have :
(DocumentClassv5 , this is the code thats attached to the flash file in the properties panel, some code ommited)
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.Stage;
import flash.text.TextFieldType;
import flash.media.Sound;
import flash.media.SoundChannel;
import miniGameOne;
import floorTileMC;
import flash.display.Loader;
import flash.net.URLRequest;
public class DocumentClassv5 extends MovieClip
{
/*#################################
## Defining Variables ##
#################################*/
public var budget:int = 0;
var gameOne:miniGameOne = new miniGameOne();
/*#################################
## Constructor ##
#################################*/
public function DocumentClassv5()
{
/*#################################
## Adding Event Listeners ##
#################################*/
trace("Document class loaded");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*###################################################
## Begins the mini game ##
###################################################*/
public function begin(evt: MouseEvent)
{
beginGame.visible = false;
beginGame.removeEventListener(MouseEvent.CLICK, begin);
budget = 500;
cleanListeners();
gameOne.loadGame();
trace(gameOne.testVar);
trace(floorTile.testVar2);
/*#################################
## Adding Event Listeners ##
#################################*/
trace("Game started");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
Then I have the miniGameOne class file, which at the moment does nothing
I also have another class file, called tileFloorMC. This is attached to a symbol.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import DocumentClassv5;
public class floorTileMC extends MovieClip
{
var propertyA:Number;
//var hackerClass:DocumentClassv5 = new DocumentClassv5;
public var testVar2:int = 50;
public function floorTileMC()
{
this.propertyA = randomRange(100, 500);
this.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver, false, 0, true);
this.addEventListener(MouseEvent.ROLL_OUT, manageMouseOut, false, 0, true);
}
private function manageMouseOver(evt: MouseEvent)
{
this.gotoAndStop(2);
//trace(mainClass.budget);
}
private function manageMouseOut(evt: MouseEvent)
{
this.gotoAndStop(1);
//mainClass.budget += 1;
}
private function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
}
}
Now, essentially I will need to be able to pass budget from DocumentClassv5 TO floorTileMC, and then BACK to DocumentClassv5. At the moment, I can pass anything from floorTileMC and anything from miniGameOne into DocumentClassv5, but when i try and pass from floorTileMC to DocumentClassv5, I get error
Error #2136: The SWF file file:///yadayada/GameV5.swf contains invalid data.
More specifically, as soon as I uncomment //var hackerClass:DocumentClassv5 = new DocumentClassv5;
Any help would be greatly appreciated!
Thanks,
Tiffany
You're trying to instantiate your document class in a subclass:
var hackerClass:DocumentClassv5 = new DocumentClassv5();
You want access to access the existing instance, not create a new one.
One thing you can do, is create a static reference to your document class. (see code sample below)
Static references can get ugly though, and you may just want to pass a reference of your doc class to your other classes when you instantiate them.
Both method below:
In your document class:
//instead of the line below:
var gameOne:miniGameOne = new miniGameOne(); //It's a bad idea to instantiate non primitive objects before the constructor of your document class runs.
//just declare it, and instantiate it in the constructor
var gameOne:miniGameOne;
//if you want to use a static reference:
public static var me:DocumentClassv5;
public function DocumentClassv5()
{
/*#################################
## Adding Event Listeners ##
#################################*/
//if using the static var me, set it's value to this (the instance of the document class):
me = this;
trace("Document class loaded");
gameOne = new miniGameOne(this); //pass a reference to the document class if NOT using the static var me
}
If using the static var me, you access it by doing the following from any class:
DocumentClassV5.me.budget;
Another cleaner alternative (if the values you need access to aren't really directly tied to any class, eg. global preferences), is make a whole new class that is just static (doesn't get instantiated) to hold your preferences.
package {
public class Global {
public static var budget:Number = 50;
}
}
Then you'd access budget by importing your Global class and doing Global.budget = 5

Pass class as constructor's argument

I'm trying to pass my object class as constructor argument. I have something like this:
package myclass {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class Example extends MovieClip {
public var _stageImg:Bitmap;
public var _stageData:BitmapData;
public var _stageClass:Class;
private var _stage:Object;
public function Example(stageClass:Class) {
_stageClass = stageClass;
}
public function createStage():Object {
_stageData = new _stageClass(0,0);
_stageImg = new Bitmap(_stageData);
_stage = addChild(_stageImg);
return _stage;
}
}
}
Now, when I try to create my Example:
import myclass.Example;
var example:Example = new Example(myObjClass);
I get message, that I'm not passing any variable (Error #1063). Why is that? What is wrong with it?
You are passing an instance of your class instead of the definition of the class itself.
In ActionScript it's a bit clunky, but you can get the class definition of an instance like this:
//first get the fully qualified classname, i.e. including the package
var className:String = getQualifiedClassName(myObjInstance);
//then get the class definition for that name
var Klass:Class = getDefinitionByName(className) as Class;
//and finally pass it to your constructor
var example:Example = new Example(Klass);
(note: I named the variable 'Klass' because 'Class' is a reserved keyword)

I need to help about " Action Script 3 , public Function with Button "

Public function PencereyiGizle ( btn:Button )
{
....
....
}
I have a problem with the btn:Button
1046: Type was not found or was not a
compile-time constant:Button..
package
{
import flash.display.MovieClip;
import flash.display.NativeWindow;
import flash.display.NativeWindowInitOptions;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;
public class PencereyiGizle extends MovieClip
{
public var natWindow:NativeWindow=new NativeWindow(
new NativeWindowInitOptions());
public var pencereyiAc_Btn:Button;
public function PencereyiGizle(fro:Button)
{
pencereAc_Btn = fro;
//Pencere ekleniyor
natWindow.width = 500;
natWindow.height = 400;
natWindow.activate();
natWindow.addEventListener(Event.CLOSING,pencereyiSakla);
pencereyiAc_Btn.label = "Pencereyi Ac";
pencereyiAc_Btn.addEventListener(MouseEvent.MOUSE_DOWN,pencereyiAktifEt);
}
//pencerenin kapanmasını engelleyip pencereyi gizliyoruz.;
private function pencereyiSakla(e:Event):void
{
e.preventDefault();
natWindow.visible = false;
}
//gizlenen pencereyi tekrar aktif hale getiriyoruz
private function pencereyiAktifEt(e:MouseEvent):void
{
natWindow.activate();
}
}
}
N AIR;
import PencereyiGizle;
var firat:PencereyiGizle= new PencereyiGizle();
addChild(firat);
and then, i get that problem "1046: Type was not found or was not a compile-time constant:Button. "
I have a problem with the btn:Button
You should use the import statement to import "Button"
Depending on your requirements it could either be
Spark Button
import spark.components.Button;
MX Button
import mx.controls.Button
UPDATE: Looks like this page has an answer to your question. Specifically check THIS answer
you problem is your class PencereyiGizle is looking for a button to be passed as a reference in the constructor which you are not doing
var firat:PencereyiGizle= new PencereyiGizle();
addChild(firat);
you need to pass either an instance name of a button
var firat:PencereyiGizle= new PencereyiGizle( someButton );
addChild(firat);
or create a new button and pass it along
var someButton:Button = new button
var firat:PencereyiGizle= new PencereyiGizle( someButton );
addChild(firat);