tweenlite is not declared - actionscript-3

package com.powerflasher.SampleApp {
import flash.events.MouseEvent;
import flash.display.Sprite;
public class test1 extends Sprite {
public function test1()
{
NewCircle1();
NewButton1();
Magic();
}
private function NewButton1():void
{
var NewButton:Sprite = new Sprite();
NewButton.graphics.beginFill(0x0000ff,1);
NewButton.graphics.drawRect(100, 100, 50, 50);
NewButton.graphics.endFill();
addChild(NewButton);
NewButton.addEventListener(MouseEvent.CLICK, Magic);
}
private function NewCircle1():void
{
var NewCircle:Sprite = new Sprite();
NewCircle.graphics.beginFill(0x000000,1);
NewCircle.graphics.drawCircle(400, 500, 50);
NewCircle.graphics.endFill();
addChild(NewCircle);
}
private function Magic():void {
Tweenlite.to(NewCircle1(), 2+Math.random()*6, {x:Math.random()*20, y:Math.random()*25, scaleX:Math.random()*3, ScaleY:Math.random()*5});
}}}
All that i'm trying to do is to draw square, draw circle, and on mouse click on square to move circle to random location
getting error "Variable 'Tweenlite' is not declared", have no idea..

There are several issues with your code, but in regards to your initial question, you need to import Tweenlite if you want to use it in your class.
import com.greensock.Tweenlite;
But you also have other problems with your code that will likely cause a problem next. You are using local variables to store your sprite instances. That means the variable name will not persist beyond the completion of those methods.
NewButton and NewCircle need to be made class properties so that they are available to the all the methods in the class.
Here's an example :
package com.powerflasher.SampleApp {
import flash.events.MouseEvent;
import flash.display.Sprite;
import com.greensock.Tweenlite; // import Tweenlite
public class test1 extends Sprite {
// declare your class properties
public var NewButton:Sprite;
public var NewCircle:Sprite;
public function test1()
{
NewCircle1();
NewButton1();
Magic();
}
private function NewButton1():void
{
NewButton = new Sprite();
NewButton.graphics.beginFill(0x0000ff,1);
NewButton.graphics.drawRect(100, 100, 50, 50);
NewButton.graphics.endFill();
addChild(NewButton);
NewButton.addEventListener(MouseEvent.CLICK, Magic);
}
private function NewCircle1():void
{
NewCircle = new Sprite();
NewCircle.graphics.beginFill(0x000000,1);
NewCircle.graphics.drawCircle(400, 500, 50);
NewCircle.graphics.endFill();
addChild(NewCircle);
}
private function Magic():void
{
Tweenlite.to(NewCircle, 2+Math.random()*6, {x:Math.random()*20, y:Math.random()*25, scaleX:Math.random()*3, ScaleY:Math.random()*5});
}
}
}

In the TweenLite function, you have - NewCircle1(). But that function doesn't return object, so error is coming, because you are trying to tween - nothing.

Related

Training Code Not working

So I am going through the book "ActionScript 3.0 Animation ~ Making Things Move" by Keith Peters, and one of the examples is teaching Parent Boxs... I have written this code out, and upon execution, it runs, but provides no Errors, nothing happens, none of the Sprites are drawn, its a blank canvas..? Using Flash Pro CS 6, 12.0.2.529. I haven't had issues with any other examples as of yet, and the .as "ParentBox" runs fine, when I try to run ParentBox2 is when I am encountering this issue.... Thoughts? (sorry, pretty new to OOP, trying to learn as much as I can, and this website in particular has ben AMAZING so far for the vast wealth of knowledge....
ParentBox.as
package {
import flash.display.Sprite;
public class ParentBox extends Sprite {
public function ParentBox()
{
init();
}
private function init():void{
graphics.lineStyle(1, 0);
graphics.drawRect(-50, -50, 100, 100);
}
}}
ParentBox2.as Code....
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class ParentBox2 extends Sprite {
private var parent1:ParentBox;
private var parent2:ParentBox;
private var ball:Sprite;
public function Reparenting2 (){
init();
}
private function init():void{
parent1 = new ParentBox();
addChild(parent1);
parent1.x = 60;
parent1.y = 60;
parent2 = new ParentBox();
addChild(parent2);
parent2.x = 170;
parent2.y = 60;
ball = new Sprite();
parent1.addChild(ball);
ball.graphics.beginFill(0xff0000);
ball.graphics.drawCircle(0, 0, 40);
ball.graphics.endFill();
ball.addEventListener(MouseEvent.CLICK, onBallClick);
}
public function onBallClick(event:MouseEvent):void{
parent2.addChild(ball);
}
}}
You have already found the answer, but for any one else having the same problem,
In ActionScript3 the constructor function should have the same name as the class name.
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class ParentBox2 extends Sprite {
private var parent1:ParentBox;
private var parent2:ParentBox;
private var ball:Sprite;
public function ParentBox2 (){ //the constructor function's name should be the same as that of the class.
init();
}
...
The
public class ParentBox2 extends Sprite {
needed to be renamed to
public class Reparenting2 extends Sprite {
for the function... Like I said, I'm still learning, especially the naming stuffs, thanks everyone!

How do I create an object using code in AS3 and give it a picture as display?

package
{
import flash.events.*;
public class declareImage extends Sprite
{
var ship:Sprite = new Sprite();
public function declareImage()
{
}
}
}
I declared an object.
Now I want to give it a background as a picture from my Compute
Should I use Sprite as data type or something else?
Here is an example. Of course, it's a very simple case (using fixed file name, etc), just to illustrate how to achieve what do you want and provide you the foundation to move on.
A tip, begin a class name with an uppercase letter.
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
public class DeclareImage extends Sprite
{
private const IMAGE_URL:String = 'myImage.jpg';
private var ship:Sprite;
private var loader:Loader;
public function DeclareImage()
{
ship = new Sprite();
addChild(ship);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgressHandler, false, 0, true);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler, false, 0, true);
loader.load(new URLRequest(IMAGE_URL));
}
private function loadProgressHandler(event : ProgressEvent) : void
{
trace('Loading: ' + Math.round((event.bytesLoaded/event.bytesTotal) * 100) + '%');
}
private function loadCompleteHandler(event:Event):void
{
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgressHandler);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadCompleteHandler);
ship.addChild(loader);
trace('complete');
}
}
}

AS3, Adding an event listener to a constructor class?

this is probably really simple I cannot for the life of me work out why this is not working. I am trying to create an object (only for testing) and assign event listeners within the constructor. In my head it should work but I am sure I must be missing something:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Box extends MovieClip {
public function Box() {
// constructor code
var mySound:Sound = new Bark();
trace("Box created");
height=800;
width=300;
x=100;
y=100;
addEventListener(MouseEvent.MOUSE_OVER, overThis);
addEventListener(MouseEvent.CLICK, clickToPlay);
}
public function overThis(m:MouseEvent){
trace("SADF");
}
function clickToPlay(m:MouseEvent){
mySound.play();
}
}}
By doing this i wanted the "box" to be self sufficient in regards to managing its own events. (Please ignore stuff like play(), that all works when I run within the MAINDOC.as directly.
This is the main doc:
package {
import flash.display.MovieClip;
import flash.media.Sound;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
public class MainDoc extends MovieClip
{
public function MainDoc()
{
// constructor code
init();
function init()
{
createBox(300,300);
}
function createBox(newX,newY)
{
var box = new Box();
box.x = newX;
box.y = newY;
addChild(box);
}
}
}}
When I test it creates the box (which i have drawn) but does not run any events?
Hope you guys can help
Ray
You have to add some graphics to your box so your MouseEvents can work:
public function Box()
{
// constructor code
var mySound:Sound = new Bark();
trace("Box created");
// begin the filling of some graphics, you can change color/alpha as you want
this.graphics.beginFill( 0x000000, 1 );
// make a rectangle 300x800
this.graphics.drawRect(0,0,300,800);
// stop filling
this.graphics.endFill();
// you don't need it anymore
//height=800;
// you don't need it anymore
//width=300;
// place your clip where you want but you do that in the Main class so no need there
//x=100;
//y=100;
// now you have graphics attached to your MovieClip the MouseEvent must work
addEventListener(MouseEvent.MOUSE_OVER, overThis);
addEventListener(MouseEvent.CLICK, clickToPlay);
}
Hope that will help you :)

AS3 Architecture : How to tween a single instance of an object on multiple calls?

I am new to actionscript and am having a trouble displaying a single instance of an object, using FlashDevelop.
I have a main.as in which I am displaying an image as background. Then I display a rectangle containing some text that tweens as the mouse hovers a target (appearing/disappearing on the stage). The rectangle is in a class TextBox.as .
I know my code is quite messy because it creates a new instance of the rectangle everytime I reach the target (calling the tween). But if I try to switch it around it gives me errors. Also I cannot seem to remove my rectangle (with removeChild()) once it is created, it cannot find the child.
Could anyone indicate me what is the architecture I should use so that only one instance of the rectangle is created?
Here's a bit of my code:
//IMPORT LIBRARIES
import Classes.TextBox;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import com.greensock.TweenLite;
// Setup SWF Render Settings
[SWF(width = "620", height = "650")]
public class Main extends Sprite
{
//DEFINING VARIABLES
[Embed(source="../lib/myimage.jpg")]
private var picture:Class;
private var myTween:TweenLite;
//CONSTRUCTOR
public function Main():void
{
addChild(new TextBox);
addChild(new picture);
addEventListener(MouseEvent.MOUSE_OVER, appear);
}
//ROLLDOWN FUNCTION
public function appear(e:MouseEvent):void
{
trace("Appear");
var text:TextBox = new TextBox();
addChild(text);
addChild(new picture);
if (picture) {
removeEventListener(MouseEvent.MOUSE_OVER, appear);
//addEventListener(Event.COMPLETE, appearComplete);
myTween = new TweenLite(text, 1, { y:340 , onComplete:appearComplete, onReverseComplete:disappearComplete} );
}
}
Thanks in advance.
i dont know what tweening you want to achieve but you should reuse your textbox instance, e.g.:
import Classes.TextBox;
import com.greensock.TweenLite;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width = "620", height = "650")]
public class Main extends Sprite {
[Embed(source="../lib/myimage.jpg")]
private var pictureClass:Class;
private var picture:Bitmap;
private var textbox:TextBox;
public function Main():void {
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
picture = new pictureClass();
textbox = new TextBox();
addChild(picture);
addChild(textbox);
addEventListener(MouseEvent.MOUSE_OVER, tween);
}
public function tween(e:MouseEvent):void {
removeEventListener(MouseEvent.MOUSE_OVER, tween);
TweenLite.to(textbox, 1, { y:340, onComplete:reverse } );
}
private function reverse():void {
TweenLite.to(textbox, 1, { y:0, onComplete:tweenComplete } );
}
private function tweenComplete():void {
addEventListener(MouseEvent.MOUSE_OVER, tween);
}
}

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.