Haxe: Return value of field - function

For some reason, I can't modify the value of a field in Haxe. Of course, this doesn't seem to be affecting all of my fields, just this one. Here's (what I'm pretty sure is) the applicable code. First, in the parent class:
class TopMenu extends Sprite
{
public function new()
{
super();
init();
}
private function init()
{
var tempField:BitmappedTextField = new BitmappedTextField( "File", 100, false );
trace( tempField.textWidth );
}
}
Then, in the child class:
class BitmappedTextField extends Sprite
{
private var _fieldText:String;
private var _fieldWidth:Int;
private var _addToStage:Bool;
public var textWidth:Int;
public function new( thisText:String, thisWidth:Int = 100, adTStg:Bool = true )
{
super();
_fieldText = thisText;
_fieldWidth = thisWidth;
_addToStage = adTStg;
textWidth = 55;
init();
}
public function init()
{
textWidth = 777;
}
}
I would expect the trace statement to return 777, but instead it will always return 55. In fact, no matter what I do, I can't seem to modify a field outside of the constructor class and then retrieve that value via the parent class. There's something horribly simple I must be missing, but I just can't figure it out. Maybe it has to do with the way Haxe uses getters and setters? Any help is appreciated, thank you.

I cant reproduce your problem however and you are missing a ; and a super call.
Try this code.
package;
import nme.display.Sprite;
import nme.display.MovieClip;
class HelloWorld extends MovieClip
{
public function new( )
{
super();
var tempField:BitmappedTextField = new BitmappedTextField();
trace( tempField.textWidth );
}
}
class BitmappedTextField extends Sprite
{
public var textWidth:Int;
public function new( )
{
super();
textWidth = 55;
init( );
}
public function init( )
{
textWidth = 777;
}
}

Related

AS3 Can't get array from another class

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));

How to call a function from a Class AS3

I'm studying to be a Game Designer, and right now I'm having some issues with my flash game.
the game has a Counter Class, with a Score Class that extends Counter. and it is added to the stage in my PlayScreenA Class.
The Timer starts with a number and decreases every 24frames. but in my Hero Class I can pick up some coins and that coins are going to increase the Time on my Score.
The question is: How I can say in my Hero Class to execute the addToValue function inside the Counter Class?
Pieces of Code:
Creating my score in the PlayScreenA Class:
private var myTime:Score = new Score();
private function create_time (){
myTime.x = 800;
myTime.y = 50;
addChild(myTime);
}
Counter Class:
package
{
import flash.display.MovieClip;
public class Counter extends MovieClip
{
public var currentValue:Number;
public function Counter()
{
reset();
}
public function addToValue( amountToAdd:Number ):void
{
currentValue = currentValue + amountToAdd;
updateDisplay();
}
public function subToValue( amountToSub:Number ):void
{
currentValue = currentValue - amountToSub;
updateDisplay();
}
public function reset():void
{
currentValue = 20;
updateDisplay();
}
public function updateDisplay():void
{
}
}
}
Score Class:
package
{
import flash.text.TextField;
import flash.events.Event;
public class Score extends Counter
{
protected var _timeCounter:int;
public function Score()
{
super();
addEventListener(Event.ENTER_FRAME, onUpdate);
}
override public function updateDisplay():void
{
super.updateDisplay();
scoreDisplay.text = currentValue.toString();
}
protected function onUpdate(e:Event):void
{
_timeCounter++;
trace(currentValue);
if (_timeCounter == 24)
{
this.subToValue( 1 );
_timeCounter = 0;
}
}
}
}
Piece of my Hero Class where I need to Call the function:
for(var i:int; i<collisionList.length;i++)
{
var $collision:platform_tile = collisionList[i];
if($hasCollided = hitbox.hitTestObject($collision.hitBox) && $collision.alpha<0.8 && $collision.alpha>0.6)
{
$collision.alpha=0;
$collision.visible = false;
//Here is where I want to call my subToValue function!
break;
}
since addToValue and subToValue are instance methods, you should provide a reference (an instance variable) to the counter object in your hero object, then call its addToValue or subToValue methods.
var theCounter:Counter;
you should either initiate this in your hero object, or assign a pre-existing counter object through getter/setters. then you can call:
theCounter.addToValue();
I assume you are trying to access the object of Score class created initially in the PlayerScreenA class...
If that is the case then you either derive a relationship between the two classes to pass this object or you could keep a static class keeping track of the function at a global level...
public class AppRefrences
{
public static var addToValueFunc:Function;
}
In Player A Class,
private var myTime:Score = new Score();
private function create_time (){
myTime.x = 800;
myTime.y = 50;
addChild(myTime);
// This is where you set the function
Apprefrences.addToValueFunc = myTime.addToValue;
}
In Hero Class,
for(var i:int; i<collisionList.length;i++)
{
var $collision:platform_tile = collisionList[i];
if($hasCollided = hitbox.hitTestObject($collision.hitBox) && $collision.alpha<0.8 && $collision.alpha>0.6)
{
$collision.alpha=0;
$collision.visible = false;
// This is where you call the function
if(AppRefrences.addToValueFunc != null)
AppRefrences.addToValueFunc(0);
break;
}
Try using getter & setters to control the update of the static variable. I didn't include it for the sake of clarity.

How to override the transform.matrix setter

I have a class which extends the Sprite object in as3. I need to be able to override the transform.matrix setter in this class but haven't been successful in doing so.
I've tried many things, along with creating my own separate class which extends the Transform class and then overrides its set matrix function, and set my transform = new CustomTransform(). Sadly this didn't work.
In code this is what i tried:
public class MyClass extends Sprite
{
public function MyClass()
{
super(); transform = new MyTransform(this);
}
}
class MyTransform extends Transform
{
public function MyTransform(dp:DisplayObject)
{
super();
}
override public function set matrix(value:Matrix)
{
super.matrix = value;
customcode();
}
}
All help is greatly appreciated!
This seems to work:
public class MyClass extends Sprite
{
public function MyClass()
{
super();
transform = new MyTransform(this,super.transform);
// i'm drawing a rect just to see the results of scaling
graphics.beginFill(0xff0000);
graphics.drawRect(0,0,100,100);
graphics.endFill();
}
override public function get transform():Transform {
var tmp:Transform;
if(super.transform is MyTransform) {
tmp = super.transform;
} else {
tmp = new MyTransform(this,super.transform);
}
return tmp;
}
override public function set transform(value:Transform):void {
var tmp:Transform;
if(value is MyTransform) {
tmp = value;
} else {
tmp = new MyTransform(this,value);
}
super.transform = tmp;
}
}
public class MyTransform extends Transform
{
public function MyTransform(dp:DisplayObject,transf:Transform = null)
{
super(dp);
if(transf) {
for(var prop:String in transf) {
this[prop] = transf[prop];
}
}
}
override public function set matrix(value:Matrix):void
{
super.matrix = value;
// customcode();
}
}
Use:
var sp:MyClass = new MyClass();
var mat:Matrix = sp.transform.matrix;
mat.scale(3,3);
trace(sp.transform);
sp.transform.matrix = mat;
addChild(sp);
The problem is that, even if you create and assign your tranform to be of type MyTransform, the getter returns a regular Transform object. There's something weird about how transform objects work in Flash (this is also true for SoundTransform, for instance). There's some kind of cache mechanism implemented in a rather lame way that forces you to reassign the instance if you want to commit your changes.
I mean this pattern:
var t:Transform = mc.transform;
// do something with t
mc.transform = t;
So I think this is related to why your code doesn't work as expected.
To get around this, I'm checking both in the setter and the getter if the trasnform object passed is of type MyTransform. If it is, I use it as is. If it's not, I create a MyTransform object and copy all of the properties from the original Transform. It'd be nice if the Transform class had a clone method, but it doesn't, so I implemented this simple copy mechanism. Not sure if this doesn't mess up with some internal state in Transform (could be the case). I haven't tested it apart from applying a scale, once. You might want to do it, as there could be other side effects I'm not considering. Also, this is probably not the most performant. But I can't think of another way to have your matrix setter called.
Edit
Using a static/global dispatcher is not a good idea except you really need it to be global. Implementing IEventDispatcher, since you can't directly extend EventDispatcher, is what you want.
The code needed for that is a bit verbose, but it's a no-brainer anyway. All you need is having an internal instance of event dispatcher and implement the methods of the interface. In said methods, you forward the parameteres to the actual dispatcher.
public class MyTransform extends Transform implements IEventDispatcher
{
private var _dispatcher:EventDispatcher;
public function MyTransform(dp:DisplayObject,transf:Transform = null)
{
super(dp);
_dispatcher = new EventDispatcher(this);
if(transf) {
for(var prop:String in transf) {
this[prop] = transf[prop];
}
}
}
override public function set matrix(value:Matrix):void
{
super.matrix = value;
// customcode();
}
public function dispatchEvent(event:Event):Boolean {
return _dispatcher.dispatchEvent(event);
}
public function addEventListener(type:String,listener:Function,useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
_dispatcher.addEventListener(type,listener,useCapture,priority,useWeakReference);
}
public function removeEventListener(type:String,listener:Function,useCapture:Boolean = false):void {
_dispatcher.removeEventListener(type,listener,useCapture);
}
public function hasEventListener(type:String):Boolean {
return _dispatcher.hasEventListener(type);
}
public function willTrigger(type:String):Boolean {
return _dispatcher.willTrigger(type);
}
}

Override Clone() in custom event for AS3... need help

I have created a custom even class which is pretty basic. But when calling an event and then relaying that event to another class I have encountered the "cannot transform thisEvent into thisOtherEvent" error.
I realize this is because I needed to override the Clone function in my custom event like so:
package com
{
import flash.disply.*;
import flash.events.Event;
public class MyCustomEvents extends Event
{
public static const SOME_EVENT:String = "some_event";
public var info:Object;
public function MyCustomEvents($type:String, $info:Object,$bubbles:Boolean = false, $cancelable:Boolean = false)
{
super($type, $bubbles, $cancelable);
this.info = $info;
}
public override function clone():Event {
return new MyCustomEvents($type, $bubbles, $cancelable);
}
}
}
However I am still getting this error when I dispatch the event. Anything else I might be missing?
here is the error:
TypeError: Error #1034: Type Coercion failed: cannot convert com.greensock.events::TransformEvent#d8df709 to com.customEvents.MyCustomEvents.
I tried casting the event in the code like so:
var deleteImgEvent:MyCustomEvent = new MyCustomEvent(MyCustomEvents.IMAGE_DELETE, {imgData: getImg}, true, false); this.dispatchEvent(deleteImgEvent as MyCustomEvents);
Still no luck.
UPDATE:
Ok, seems like the problem is in the greensock Transform library. When the event handler for my custom event is called, I run a function of the TransformManager class.
_manager.deleteSelection();
Inside that class it dispatched a TransformEvent. Not sure why, but it is reading that delete event as a MyCustomEvent.
/**
* #usage
* var myEvent:CustomEvent = new CustomEvent(CustomEvent.EVENT_TYPE_A, { integerRelatedToEvent: 5, stringRelatedToEvent: 'easy' });
* addEventListener(CustomEvent.EVENT_TYPE_A, traceCustomEvent);
* dispatch(myEvent);
* function traceCustomEvent ($e:CustomEvent):void {
* trace($e.type);
* }
*/
package {
import flash.events.Event;
public class CustomEvent extends Event {
// Types:
public static const EVENT_TYPE_A:String = 'CustomEvent.EVENT_TYPE_A';
public static const EVENT_TYPE_B:String = 'CustomEvent.EVENT_TYPE_B';
// Components:
private var _customDatum:Object;
public function get customDatum ():Object { return _customDatum; }
public function CustomEvent ($type:String, $customDatum:Object) {
super($type);
_customDatum = $customDatum;
}
public override function clone ():Event {
return new CustomEvent(type, _customDatum);
}
}
}
"When creating your own custom Event
class, you must override the inherited
Event.clone() method in order for it
to duplicate the properties of your
custom class. If you do not set all
the properties that you add in your
event subclass, those properties will
not have the correct values when
listeners handle the redispatched
event."
package com.events;
{
import flash.events.Event;
public class XMLLoaderEvent extends Event
{
public static const XML_LOADED:String = "XML_Loaded";
public var data:*;
public var properties:Object;
public function XMLLoaderEvent( type:String,_data:*,bubbles:Boolean = false,cancelable:Boolean = false):void
{
super( type, bubbles, cancelable );
data = _data;
}
// Override clone
override public function clone():Event
{
return new XMLLoaderEvent( type, data, bubbles, cancelable);
}
}
}
Don't know if that's it but you have an extra parameter $info:Object into your custom event, but you don't pass it in your clone contructor.
return new MyCustomEvents(type, info, bubbles, cancelable);
I think you need the clone function to return a MyCustomEvents type. Not an Event type. And you need to add the info parameter as stated by the previous poster.
package com {
import flash.display.*;
import flash.events.Event;
public class MyCustomEvents extends Event {
public static const SOME_EVENT:String = "some_event";
public var info:Object;
public function MyCustomEvents($type:String, $info:Object,$bubbles:Boolean = false, $cancelable:Boolean = false) {
super($type, $bubbles, $cancelable);
this.info = $info;
}
public override function clone():MyCustomEvents {
return new MyCustomEvents(this.type, this.info, this.bubbles, this.cancelable);
}
}
}

AS3: overriding protected var in subclass

I'm having a blackout here. I thought I understood these principles, but I can't seem to get it working anymore. I want to let a DeleteButton inherit from a general Button class. This DeleteButton should alter the protected padding values and have a static label. This is what I have:
public class Button
{
private var _labelText:String;
protected var _paddingX:Number = 10;
protected var _paddingY:Number = 5;
public function Button( labelText:String ):void
{
_labelText = labelText;
}
}
public class DeleteButton extends Button
{
public function DeleteButton():void
{
_paddingX = 5;
_paddingY = 2;
super( 'x' );
}
}
Now, I thought the altered _paddingX and _paddingY values in the inherited class would bubble up to the super class. But they don't. The DeleteButton is still constructed with the default values of the super class. I can't figure out how to do it anymore. What am I missing here?
Thanks in advance.
Edit:
I guess I had AS3's implementation mixed up with PHP's. This 'equivalent' example in PHP is perfectly legal:
class Button
{
protected $_paddingX = 10;
protected $_paddingY = 5;
public function __construct( $label = '' )
{
var_dump( $label . ':' );
var_dump( $this->_paddingX );
var_dump( $this->_paddingY );
echo '<br>';
}
}
class DeleteButton extends Button
{
public function __construct()
{
$this->_paddingX = 5;
$this->_paddingY = 2;
parent::__construct( 'x' );
}
}
$button = new Button( 'browse' );
$deleteButton = new DeleteButton();
// this outputs:
string(7) "browse:" int(10) int(5)
string(2) "x:" int(5) int(2)
when you call the super() function you also initialise all of the super classes variables. So in your example when you call..
_paddingX = 5;
_paddingY = 2;
..before calling super(), you are actually creating arbitrary values inside that function. So you're getting it right its just that those variables dont exist until the super() function has been called.
public class Button
{
private var _labelText:String;
protected var _paddingX:Number;
protected var _paddingY:Number;
public function Button( labelText:String, paddingX:Number=10, paddingY:Number=5 ):void
{
_paddingX = paddingX;
_paddingY = paddingY;
_labelText = labelText;
}
public function traceVars():void {
trace(_labelText);
trace(_paddingX);
trace(_paddingY);
}
}
public class DeleteButton extends Button
{
public function DeleteButton():void
{
super( 'x', 5, 2 );
}
}
Do this instead, this way you can keep the default values, as 10 and 5, but change them if needed. Now if you call this on the main timeline you it should work for you.
var but:DeleteButton = new DeleteButton();
but.traceVars();
Hope this explains it :)
George
I think calling super('x') before setting the _paddingX and _paddingY could make a difference:
public function DeleteButton():void
{
super( 'x' );
_paddingX = 5;
_paddingY = 2;
}
What you could do is to override getters:
public class Button
{
private var _labelText:String;
protected var _paddingX:Number = 10;
protected var _paddingY:Number = 5;
public function Button( labelText:String ):void
{
_labelText = labelText;
}
protected function get paddingX():Number
{
return _paddingX;
}
// get paddingY ommited for brevity
}
public class DeleteButton extends Button
{
public function DeleteButton():void
{
super( 'x' );
}
override protected function get paddingX():Number
{
return 42;
}
}
The parent Button constructor will now use the overridden getter from child DeleteButton class.
You could also set _paddingX/Y to private to hide them from the implementation, and add new _paddingX/Y in DeleteButton. This however, seems like a dirty practice, because the properties in DeleteButton aren't enforced by API.