Motion that doesn't work inside function - actionscript-3

So I have this motion script which I copied from Cs6 as "Copy Motion as AS3". It works fine until I tried to control it with a mouse event function. So what am I missing? it works fine outside the function but not at all inside.
import fl.motion.AnimatorFactory;
import fl.motion.MotionBase;
import fl.motion.Motion;
import flash.filters.*;
import flash.geom.Point;
var __motion_NewBall_7: MotionBase;
theButton.addEventListener(MouseEvent.CLICK, on_press1);
function on_press1(event: MouseEvent): void {
if (__motion_NewBall_7 == null) {
__motion_NewBall_7 = new Motion();
__motion_NewBall_7.duration = 60;
__motion_NewBall_7.addPropertyArray("x", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4.02143, -8.04286, -12.0603, -16.0857, -20.1071, -24.1246, -28.15, -35.6987, -43.2474, -50.7885, -58.3447, -65.8934, -73.4346, -80.9832, -88.5395, -96.0882, -103.637, -111.186, -118.727, -126.283, -133.824, -141.38, -148.929, -156.478, -164.026, -171.575, -179.124, -186.672, -194.221, -201.77, -209.311, -216.867, -224.416, -231.957, -239.506, -247.062, -254.611, -262.152, -269.708, -277.257, -284.805, -292.346, -299.903, -307.451, -315]);
__motion_NewBall_7.addPropertyArray("y", [0, 9.41071, 18.8214, 28.2227, 37.6429, 47.0536, 56.4549, 65.875, 75.2857, 84.6964, 94.1071, 103.518, 112.919, 122.339, 131.75, 124.157, 116.564, 108.979, 101.379, 93.7857, 86.2004, 78.6, 84.0237, 89.4474, 94.8656, 100.295, 105.718, 111.137, 116.56, 121.989, 127.413, 132.837, 138.261, 143.679, 149.108, 154.526, 159.955, 165.379, 170.803, 176.226, 181.65, 187.074, 192.497, 197.921, 203.345, 208.763, 214.192, 219.616, 225.034, 230.458, 235.887, 241.311, 246.729, 252.158, 257.582, 263.005, 268.424, 273.853, 279.276, 284.7]);
__motion_NewBall_7.addPropertyArray("scaleX", [1.000000]);
__motion_NewBall_7.addPropertyArray("scaleY", [1.000000]);
__motion_NewBall_7.addPropertyArray("skewX", [0]);
__motion_NewBall_7.addPropertyArray("skewY", [0]);
__motion_NewBall_7.addPropertyArray("rotationConcat", [0]);
__motion_NewBall_7.addPropertyArray("blendMode", ["normal"]);
__motion_NewBall_7.addPropertyArray("cacheAsBitmap", [false]);
__motion_NewBall_7.addPropertyArray("opaqueBackground", [null]);
__motion_NewBall_7.addPropertyArray("visible", [true]);
var __animFactory_NewBall_7: AnimatorFactory = new AnimatorFactory(__motion_NewBall_7);
__animFactory_NewBall_7.transformationPoint = new Point(0.500000, 0.500000);
__animFactory_NewBall_7.addTarget(myClip, 0);
}
}

Here is the solution to the problem: I had to break var __animFactory_NewBall_7: AnimatorFactory = new AnimatorFactory(__motion_NewBall_7); into two sections: var __animFactory_NewBall_7: AnimatorFactory; to be outside the function and var __animFactory_NewBall_7: AnimatorFactory = new AnimatorFactory(__motion_NewBall_7);to be inside the function
import fl.motion.AnimatorFactory;
import fl.motion.MotionBase;
import fl.motion.Motion;
import flash.filters.*;
import flash.geom.Point;
theButton.addEventListener(MouseEvent.CLICK, on_press1);
var __motion_NewBall_7: MotionBase;
var __animFactory_NewBall_7: AnimatorFactory; // <-just the declaration was to be moved. not the definition
function on_press1(event: MouseEvent): void {
if (__motion_NewBall_7 == null) {
__motion_NewBall_7 = new Motion();
__motion_NewBall_7.duration = 60;
__motion_NewBall_7.addPropertyArray("x", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4.02143, -8.04286, -12.0603, -16.0857, -20.1071, -24.1246, -28.15, -35.6987, -43.2474, -50.7885, -58.3447, -65.8934, -73.4346, -80.9832, -88.5395, -96.0882, -103.637, -111.186, -118.727, -126.283, -133.824, -141.38, -148.929, -156.478, -164.026, -171.575, -179.124, -186.672, -194.221, -201.77, -209.311, -216.867, -224.416, -231.957, -239.506, -247.062, -254.611, -262.152, -269.708, -277.257, -284.805, -292.346, -299.903, -307.451, -315]);
__motion_NewBall_7.addPropertyArray("y", [0, 9.41071, 18.8214, 28.2227, 37.6429, 47.0536, 56.4549, 65.875, 75.2857, 84.6964, 94.1071, 103.518, 112.919, 122.339, 131.75, 124.157, 116.564, 108.979, 101.379, 93.7857, 86.2004, 78.6, 84.0237, 89.4474, 94.8656, 100.295, 105.718, 111.137, 116.56, 121.989, 127.413, 132.837, 138.261, 143.679, 149.108, 154.526, 159.955, 165.379, 170.803, 176.226, 181.65, 187.074, 192.497, 197.921, 203.345, 208.763, 214.192, 219.616, 225.034, 230.458, 235.887, 241.311, 246.729, 252.158, 257.582, 263.005, 268.424, 273.853, 279.276, 284.7]);
__motion_NewBall_7.addPropertyArray("scaleX", [1.000000]);
__motion_NewBall_7.addPropertyArray("scaleY", [1.000000]);
__motion_NewBall_7.addPropertyArray("skewX", [0]);
__motion_NewBall_7.addPropertyArray("skewY", [0]);
__motion_NewBall_7.addPropertyArray("rotationConcat", [0]);
__motion_NewBall_7.addPropertyArray("blendMode", ["normal"]);
__motion_NewBall_7.addPropertyArray("cacheAsBitmap", [false]);
__motion_NewBall_7.addPropertyArray("opaqueBackground", [null]);
__motion_NewBall_7.addPropertyArray("visible", [true]);
__animFactory_NewBall_7 = new AnimatorFactory( __motion_NewBall_7); // define it after the motion, _motion_NewBall_7 exists
__animFactory_NewBall_7.transformationPoint = new Point(0.500000, 0.500000);
__animFactory_NewBall_7.addTarget(myClip, 1);
}
}

Related

AS3 Instance Name of a custom class

I'm trying to get the instance name from a dropTarget of an object.
import flash.display.Sprite;
import flash.events.MouseEvent;
import com.components.DragDropBucket;
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0xFFCC00);
circle.graphics.drawCircle(0, 0, 40);
var target:Array = new Array();
target[0] = new Sprite();
target[0].graphics.beginFill(0xCCFF00);
target[0].graphics.drawRect(0, 0, 100, 100);
target[0].name = "target1";
target[1] = new Sprite();
target[1].graphics.beginFill(0xCCFF00);
target[1].graphics.drawRect(0, 200, 100, 100);
target[1].name = "target2";
var testv:DragDropBucket = new DragDropBucket("test", 500, 500, 175, 40,false);
testv.name ="test2";
var test:DragDropBucket = new DragDropBucket("test", 0, 0, 175, 40,true);
addChild(target[0]);
addChild(target[1]);
addChild(circle);
addChild(testv);
addChild(test);
circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown)
function mouseDown(event:MouseEvent):void {
circle.startDrag();
}
circle.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
function mouseReleased(event:MouseEvent):void {
circle.stopDrag();
trace(circle.dropTarget.name);
}
DragDropBucket is a custom class that extends Sprite Class.
So when I execute this code, when I drop the circle to target[0] or target[0] it successfully output "target1" and "target2".
Meanwhile when im drop the circle to testv object, the program output its name as instance#
Yet when I execute this code
trace(testv.name);
It prints "test2" well, no instance# or anything.
I'm not understand what is happening here, since DragDropBucket Class only extending the Sprite class and have nothing fancy. Maybe someone here can enlighten me, Thanks.
Your DragDropBucket class is not different of a library class. It's the same logic. There's an object contained in 'testv', your instance of DragDropBucket class, that is being detected as dropTarget.
function mouseReleased(event:MouseEvent):void {
circle.stopDrag();
trace(circle.dropTarget.name, circle.dropTarget.parent.name);
// output : instance1, test2
}
If 'test2' is not the parent of 'instance1', that's its parent.parent, or more.

Fade In And Out In ActionScript 3

I'm new to ActionScript 3 and I'm having a basic problem. I'm trying to fade one of my variables in and out but it's just fading in. It's tween3. Can you help?
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.events.TimerEvent;
import fl.transitions.TweenEvent;
var timer:Timer = new Timer(3000);
timer.start();
var tween2:Tween = new Tween(main, "x", Strong.easeOut, main.x, 0, 2, true);
var tween1:Tween = new Tween(his, "alpha", None.easeOut, 1, 0, 1, true);
var tween3:Tween = new Tween(her, "alpha", Strong.easeInOut, 0, 1, 2, true);
var tween4:Tween = new Tween(gilt, "alpha", Strong.easeIn, 0, 1, 2, true);
tween1.stop();
tween2.stop();
tween3.stop();
tween4.stop();
timer.addEventListener(TimerEvent.TIMER, startTween);
function startTween(event:TimerEvent):void {
tween1.start();
tween2.start();
tween3.start();
tween4.start();
}
timer.addEventListener(TimerEvent.TIMER, stopTimer);
function stopTimer(event:TimerEvent):void {
timer.stop();
}
Here's an example of some code that does, more or less, what you're trying to do. I'm sure that you can learn from it and adapt this to your code.
To test my code make three MovieClips and put 'em on stage with instance names 'boxA', 'boxR' and 'blackbtn' Clicking on the button will start your timer and tweens. In my case the 'boxes' are in an array amd the tweens are created in a 'for' loop, but of course, they don't have to be.
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.events.TimerEvent;
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, timerListener);
blackbtn.addEventListener(MouseEvent.CLICK, startit)
var A:Array = new Array();
A.push(boxB); A.push(boxR);
function timerListener(e)
{
for(var i:int = 0; i < A.length; i++)
{
new Tween(A[i], "alpha", Regular.easeOut, 1, 0, 1, true);
}
}
function startit(e)
{
timer.start();
}
You can use TweenMax framework instead of tween3 because Tweenmax has lots of feature (sometimes is better than Flash Timeline) also here is your solution:
var t:TweenMax = new TweenMax(main,1,{ x: 100, delay:1,ease: Strong.easeOut });
t.start();
stage.addEventListener(MouseEvent.CLICK, onClickedStage);
function onClickedStage(event:MouseEvent):void
{
t.stop();
}

Is it possible to call a variable from another function in Actionscript 3.0?

import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer = new Timer(1000);
start_btn.buttonMode = true;
stop_btn.buttonMode = true;
start_btn.addEventListener(MouseEvent.CLICK, onStart, false, 0, true);
timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
//stage.addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
function onStart(evt:MouseEvent):void
{
var minutes:Number = Number(min_txt.text);
var seconds:Number = Number(sec_txt.text);
timer.start();
}
function onTimer(evt:TimerEvent):void
{
minutes--;
trace("Timer Triggered!!");
}
So how do i make it so that the "minutes--" works ..as the variables are in a seperate function..
(or give me another way)..
Thanks..
if you declare variable in function, is a local variable. you not access variable other function, other scope. but if you declare in global variable. available anywhere.
Easy way, if you variable declared globally. It's available.
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer = new Timer(1000);
start_btn.buttonMode = true;
stop_btn.buttonMode = true;
start_btn.addEventListener(MouseEvent.CLICK, onStart, false, 0, true);
timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
//stage.addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
var minutes:Number;
function onStart(evt:MouseEvent):void
{
minutes = Number(min_txt.text);
var seconds:Number = Number(sec_txt.text);
timer.start();
}
function onTimer(evt:TimerEvent):void
{
minutes--;
trace("Timer Triggered!!");
}

AS3: Pick color from object

I make some object and colored with Color Transform. Here's my code:
function createColorItems():void
{
for (var i:int = 0; i < colorLength; i++)
{
var myColor:Object = new colorArea ;
var colorTrans:ColorTransform = new ColorTransform ;
arrColorTrans[i] = myXML.bag.color.item[i];
arrItem.push(myColor);
arrItem[i].x = 40 * i + 40;
arrItem[i].y = 300;
addChild(arrItem[i]);
colorTrans.color = Number(arrColorTrans[i]);
arrItem[i].transform.colorTransform = colorTrans;
arrItem[i].addEventListener(MouseEvent.CLICK,changeColor);
}
}
This is where i change the color.
function changeColor():void
{
trace(e.target.color);
myBox.graphics.beginFill(0x000000,0.5);
myBox.graphics.drawRect(myImg.x,myImg.y,bagImg.width,bagImg.height);
myBox.graphics.endFill();
myBox.transform.colorTransform = publicColor;
addChild(myBox);
}
what i want is when the object is clicked, the other object's color is change. I trace it with trace(e.target.color) but it's wrong. i use publicColor to pick the color from colorTrans, but i don't know how to pick the color? is it possible??
Sorry for my bad grammar, please help.
You can use getPixel method of BitmapData. Here is a documentation. Example of using:
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.display.Sprite;
var s0:Sprite = new Sprite();
var s1:Sprite = new Sprite();
var testS:Sprite = new Sprite();
var bmd:BitmapData = new BitmapData(1,1);
s0.graphics.beginFill(0x010FFF);
s0.graphics.drawRect(0, 0, 100, 100);
s0.graphics.endFill();
s0.x = 200;
s0.y = 200;
s1.graphics.beginFill(0x555FFF);
s1.graphics.drawRect(0, 0, 100, 100);
s1.graphics.endFill();
s1.x = 300;
s1.y = 200;
addChild(s0);
addChild(s1);
addChild(testS);
addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void{
const clicked:DisplayObject = event.target as DisplayObject;
bmd.fillRect(bmd.rect, 0x000000);
bmd.draw(clicked, new Matrix(1, 0, 0, 1, clicked.x - mouseX, clicked.y - mouseY));
const color:uint = bmd.getPixel(0, 0);
testS.graphics.beginFill(color);
testS.graphics.drawRect(0, 0, 100, 100);
testS.graphics.endFill();
trace("color:" + color);
}
BitmapData.getPixel(x:int, y:int):uint
or
BitmapData.getPixel32(x:int, y:int):uint
Returns an ARGB color value that contains alpha channel data and RGB data.

Sending Image in AS3

I'm trying to send an image in as3, since some images are to big, I tried to split the image apart and then send them, after that reassemble them. This is the code:
The Sender Code:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.DatagramSocketDataEvent;
import flash.events.Event;
import flash.net.DatagramSocket;
import flash.net.FileReference;
import flash.utils.ByteArray;
public class test extends Sprite
{
private var socket:DatagramSocket;
private var r:FileReference;
private var loader:Loader;
public function test()
{
socket = new DatagramSocket();
socket.bind(12395, "127.0.0.1");
r = new FileReference();
r.addEventListener(Event.SELECT, loadFile);
r.browse();
}
protected function loadFile(event:Event):void
{
r.addEventListener(Event.COMPLETE, sendFile);
r.load();
}
protected function sendFile(event:Event):void
{
var b:ByteArray;
trace(r.data.bytesAvailable);
for(var i:uint = 0; i < r.data.bytesAvailable;)
{
b = new ByteArray();
if(r.data.bytesAvailable >= 1024)
{
r.data.readBytes(b, 0, 1024);
socket.send(b, 0, 0, "127.0.0.1", 12345);
trace(b.bytesAvailable);
trace(r.data.bytesAvailable);
}
else
{
r.data.readBytes(b, i, r.data.bytesAvailable);
socket.send(b, 0, 0, "127.0.0.1", 12345);
b = new ByteArray();
socket.send(b, 0, 0, "127.0.0.1", 12345);
trace(b.bytesAvailable);
trace(r.data.bytesAvailable);
break;
}
}
trace("----------------------------");
}
}
}
The Receiver Code:
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.DatagramSocketDataEvent;
import flash.events.Event;
import flash.net.DatagramSocket;
import flash.net.FileReference;
import flash.utils.ByteArray;
var socket:DatagramSocket;
var loader:Loader;
var b:ByteArray;
var i:uint = 0;
b = new ByteArray();
socket = new DatagramSocket();
socket.bind(12345, "127.0.0.1");
socket.addEventListener(DatagramSocketDataEvent.DATA, handleData);
socket.receive();
function handleData(event:DatagramSocketDataEvent):void
{
if(event.data.bytesAvailable != 0)
{
trace("writing bytes...");
event.data.readBytes(b, i);
trace(b.bytesAvailable);
//b.length = b.length + 1;
i = i + 1024;
}
else
{
loadImg();
}
}
function loadImg():void
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, addToStage);
loader.loadBytes(b);
}
function addToStage(event:Event):void
{
addChild(loader);
}
But the image does't load, even though the sent and the received bytes are exactly equal.
I just simply modify on sender code as below:
else
{
r.data.readBytes(b, i, r.data.bytesAvailable);
socket.send(b, 0, 0, "127.0.0.1", 12345);
b = new ByteArray();
b.writeByte(0) // ADD THIS NEW LINE
socket.send(b, 0, 0, "127.0.0.1", 12345);
trace(b.bytesAvailable);
trace(r.data.bytesAvailable);
break;
}
And it will fire the loadImg function.