Using tweenlite in action script 3 - actionscript-3

I am trying to make a square get bigger using tweenlite (from greensock).
Using the following line but it gives an error, any ideas?
public function getStarted ():void {
var juiceBox:Shape = new Shape();
var newValue:Number = new Number();
newValue = 0;
juiceBox.graphics.beginFill(0xcccccc);
juiceBox.graphics.drawRect(0, squareHeight-newValue, squareWidth, newValue);
juiceBox.graphics.endFill();
square.addChild(juiceBox);
var myTween:TweenLite = new TweenLite(juiceBox.graphics.drawRect, 5, {"y":squareHeight-60,"height":60 });
}
[Fault] exception, information=ReferenceError: Error #1069: Property y not found on builtin.as$0.MethodClosure and there is no default value.
Fault, PropTween() at PropTween.as:58
Thanks any help would be greatly appreciated.

You are tweening "juiceBox.graphics.drawRect" which is a function (class method). That class method has no property named 'y' or 'height'. I'm guessing what you want to tween is juicebox itself (that has such properties) so you would do:
TweenLite.to(juicebox, 5, etc ...
You are supposed to use the form TweenLite.to() (which return an instance) and not the constructor.
var myTween:TweenLite = TweenLite.to(etc ...)
This keeps the tween from being GC.

Related

Actionscript 3: How can I change the name property of content returned by Loader.load()? Error 1074

I have a Scaleform movie that I want to serve as the container for my game's user interface. I want it to be able to load and unload other swf files that will serve as different HUDs and menus. And when I load a swf, I need Actionscript to register the name of the DisplayObject, so the game will know which "view" (i.e., HUD, pause menu, shop menu, etc.) just loaded.
I am able to load other swfs using Loader.load(), but for some reason I can't change their names. I keep getting error 1074.
[Edit: Adding more info on the error. "Error #1074: Illegal write to read-only property." Apparently I'm trying to write to a read-only property. So how do I make that property not-read-only? name isn't read-only in any other UIComponents I'm loading.]
public function loadView(viewName:String, movieFileName:String):void
{
var loader:Loader = new Loader();
var url:URLRequest = new URLRequest(movieFileName);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
loader.name = viewName;
loader.load(url);
}
private function loaderComplete(e:Event):void
{
var loader:Loader = e.currentTarget.loader as Loader;
var content:DisplayObject = LoaderInfo(e.target).content; // This returns the content I'm looking for, but I always get error 1074 if I try changing its name
// var content:DisplayObject = loader.getChildAt(0); // This also returns the content I'm looking for, but it also gives me error 1074 if I try changing its name
// content.name = loader.name; // This line always gives me error 1074
// var newView:View = View(content); // Even if I try casting the content as a custom .as class...
// newView.setName(loader.name); // public function setName(newName:String):void { this.name = newName; } // ...I still get error 1074
addChild(content);
}
Am I just not allowed to change the name property of swf movies that get returned? Can I set the name in the document class of the swf? I tried that too, but no matter where I change the name inside the document class (their class extends scaleform.clik.core.UIComponent, and I try setting the name in the constructor and in configUI), it always seems to get overwritten when I addChild().
[And another edit. Apparently there is some confusion over the "name" property. Here's how it works...]
I start off with this code. I just put it in frame 1 of my movie.
import TestUIComponent;
var testUIComponent:TestUIComponent = new TestUIComponent();
testUIComponent.name = "Something something";
trace("This is the testUIComponent's name: " + testUIComponent.name);
addChild(testUIComponent);
This is the class TestUIComponent:
package {
import scaleform.clik.core.UIComponent;
public class TestUIComponent extends UIComponent {
public function TestUIComponent() {
}
override protected function configUI():void {
super.configUI();
enableInitCallback = true;
}
}
}
Nothing fancy there. It's just an Actionscript 3 scaleform.clik.core.UIComponent (need to specify that because I think there are at least 3 different UIComponents in different packages). enableInitCallback is a property that used to be visible in Flash's properties panel, but now in AS 3, it seems you can only change it in code.
So I run that code, and this is what I see:
This is the testUIComponent's name: Something something
CLIK Load: root.Something something
If I comment out the line
// testUIComponent.name = "Something something";
and then run the code, this is what I see:
This is the testUIComponent's name: instance1
CLIK Load: root.instance1
Going back to my original problem, the text that comes after "CLIK Load:" is the name that is getting sent from the UI to the game. I need that name to be something meaningful so the game knows what just got loaded. The swf files I am trying to load have Document Classes that are children of scaleform.clik.core.UIComponent, so I thought their name properties would work the same way as the TestUIComponent above. Apparently it doesn't. And as you can see all the way back up at the top, I even cast the loader.content as a View (which is a child of UIComponent), and I still can't change the name.
This is what I meant in the comments. Try something like this:
//... where you declare your variables, make them public to use in other functions...
public var myString = "";
//... later where you declare functions...
public function loadView(viewName:String, movieFileName:String):void
{
var loader:Loader = new Loader();
var url:URLRequest = new URLRequest(movieFileName);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
//loader.name = viewName;
myString = viewName; //# 1) update String here to re-access in next function...
loader.load(url);
}
private function loaderComplete(e:Event):void
{
var content:DisplayObject = LoaderInfo(e.target).content; // This returns the content I'm looking for, but I always get error 1074 if I try changing its name
// content.name = loader.name; // This line always gives me error 1074
content.name = myString; //# 2) set content name to whatever myString holds (ie the viewName)...
addChild(content);
}

as3 error: access of possibaly undefind property through a reference with static type flash.display:DisplayObject

I have this as3 project, and in frame one of the timeline I tried to load a swf movie named "menu" and in this loaded movie I have an instance of a button named "button1", and I want to add a new EventListener to this "button1". my code is here:
var theLoader:Loader = new Loader();
var address:URLRequest = new URLRequest("menu.swf");
theLoader.load(address);
theLoader.contentLoaderInfo.addEventListener(Event.COMPLETE , swfDidLoad);
function swfDidLoad(evt:Event){
if(theLoader.content){
addChild(theLoader);
var button:SimpleButton = theLoader.content.button1;
button.addEventListener(MouseEvent.CLICK, handler1);
}
}
function handler1 (event:MouseEvent):void
{
removeChild(theLoader);
gotoAndStop(10);
};
but I get this undefind property error. what should I do? Am i doing this right at all?
The reason you are getting that error is because you are trying to access button1 on theLoader.content which is a non-dynamic DisplayObject (this means that only explicitly defined properties/methods are valid). You must first cast it to a MovieClip (which is dynamic).
You should change that line to:
var button:SimpleButton = MovieClip(theLoader.content).button1;

Adding ID to a new created object as3

i got a question , i create a new child, a circle but i dont know how can i give it an ID, so i can access it whenever i want, even if i move it , the problem is my function new_sond creates more than 1 object, so i want to give them the ID in the function for example for the 1 object "1" for the 2nd "2" and so on, i dont have any idea how to do it, i tried to search but didnt find anything, the trace(name) won`t be usefull becouse i create more objects with the same name...
here is the code for creating the object :
function new_sond(event:MouseEvent):void
{
if (i<9)
{
i++;
id[i]=i;
var btn:Sprite = new Sprite();
btn.graphics.beginFill(0x0066FF, 1);
btn.graphics.drawCircle(400, 300, 25);
btn.graphics.endFill();
var textField = new TextField();
textField.mouseEnabled=false;
textField.text = i;
textField.width = 10;
textField.height = 17;
textField.x = 395; // center it horizontally
textField.y = 292; // center it vertically
cx[i]=textField.x;
cy[i]=textField.y;
btn.addChild(textField);
this.addChild(btn);
}
}
And this is the code for moving the object :
this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownH);
this.addEventListener(MouseEvent.MOUSE_UP, mouseUpH);
function mouseDownH(evt:MouseEvent):void {
var object = evt.target;
object.startDrag();
}
function mouseUpH(evt:MouseEvent):void {
var obj = evt.target;
obj.stopDrag();
}
The question is how do i give an ID to each created object so i can check it even if i move the object.
Thank you very much !!!!
You can set the name property on the Sprite class. This property is inherited from the DIsplayObject class. Here is a summary of the property from the documentation.
The property is a String and you set or retrieve it from its setter/getter implementations in DisplayObject:
public function get name():String
public function set name(value:String):void
This property is part of ActionScript 3.0 and is available in runtime versions starting with AIR 1.0, Flash Player 9, Flash Lite 4 (which means it is available in later version as well).
It can throw an IllegalOperationError though. This is thrown if you attempt to set the property on an object placed on the timeline via the Flash authoring tool.
Here is the example given in the DisplayObject#name property documentation. The example creates two Sprite objects and traces their names when they are clicked.
import flash.display.Sprite;
import flash.events.MouseEvent;
var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFF0000);
circle1.graphics.drawCircle(40, 40, 40);
circle1.name = "circle1";
addChild(circle1);
circle1.addEventListener(MouseEvent.CLICK, traceName);
var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x0000FF);
circle2.graphics.drawCircle(140, 40, 40);
circle2.name = "circle2";
addChild(circle2);
circle2.addEventListener(MouseEvent.CLICK, traceName);
function traceName(event:MouseEvent):void {
trace(event.target.name);
}
If this does not work for you, you can always create your own class that is a sub-class of Sprite and add your own properties to track an "id" field for whatever purposes you seek.
Or you could put your objects in an array and rely on their array position as an Id.
I just stumbled on this question and thought it relevant to point out that AS3 also has a built-in utility for generating unique names for Objects
NameUtil.createUniqueName

AS3 objects (buttons) from library not accessible from within certain functions

I'm working on a simple image processor in AS3 and as usual I almost had it finished when an annoying little problem appeared. I want to open an image using a dialog box, and once the image is open I want the "selectBtn" (which is in my Library) to disappear. However when I try the code below I get a: "Error #1009: Cannot access a property or method of a null object reference". I'm sure it's something simple but I just can't figure it out!
// declare variables
var image:Bitmap;
var loader:Loader = new Loader();
var fileRef:FileReference= new FileReference();
selectBtn.addEventListener(MouseEvent.CLICK, openImage);
function openImage(event:MouseEvent):void {
fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);
fileRef.addEventListener(Event.SELECT, onFileSelected);
}
function onFileSelected(e:Event):void {
fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
fileRef.load();
}
function onFileLoaded(e:Event):void {
loader.loadBytes(e.target.data);
image = Bitmap(loader.content);
selectBtn.visible = false;
}
What Todd says is accurate: where is "selectBtn" defined? Is it on a stage, or nested within some movieclip instance?
Assuming the button exists on the stage, and the code runs in the same frame as the button exists, you could try this.stage.selectBtn.visible = false;
Otherwise, trace that you have selectBtn as a reference. Or you could declare a variable to reference it. i.e.:
var selectBtnRef:Button = this.stage.selectBtn;

Timer in Action Script 3

I have problem that I need to create timer but I want to pass on a variable to it, how to do it? Is it possible in AS3?
I tried like this:
bonusPlayer1Timer = new Timer(5000);
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER, bonusChanges(player1));
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER_COMPLETE, bonusChangesRemove(player1));
bonusPlayer1Timer.start();
function bonusChanges(event:TimerEvent, playerBonus:Player):void {
switch (playerBonus.bonus) {
case 0 :
playerBonus.multipleShooting = false;
playerBonus.bonus = -1;
break;
...}}
But I have error:
1067: Implicit coercion of a value of type Player to an unrelated type flash.events:TimerEvent.
1136: Incorrect number of arguments. Expected 2.
And this error is in the bold line.
Can I use it in this way? Or I have to create two the same function for every of my players because I am not allow to pass on any different arguments to the timer function?
Thank you,
Create a class that extends the Timer class and add a property for the Player.
public class PlayerTimer extends Timer
{
public var thePlayer:Player;
public function PlayerTimer(delay:Number, repeatCount:int=0)
{
super(delay, repeatCount);
}
}
Using your example the code would be something like this:
bonusPlayer1Timer = new PlayerTimer(5000);
bonusPlayer1Timer.thePlayer = new Player();
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER, bonusChanges);
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER_COMPLETE, bonusChangesRemove);
bonusPlayer1Timer.start();
function bonusChanges(event:TimerEvent):void {
var playerBonus:Player = PlayerTimer(event.target).thePlayer;
switch (playerBonus.bonus) {
case 0 :
playerBonus.multipleShooting = false;
playerBonus.bonus = -1;
break;
...}}
You can never pass more than one argument into the function triggered by an EventListener. You need to find other ways of passing your information around, such as the solution provided by Nathan Smith.
you can also do it this way:
var x = setTimeout(yourfunction(/*arguments you need*/),1000);
function yourfunction(/*var*/){
//your code
}
Just Type
var bonusPlayer1Timer = new Timer(5000);