Fade In And Out In ActionScript 3 - 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();
}

Related

How to decrement Timer variable - AS3

Well, I have two TIMER type variables in my code AS3, but there comes a certain part
of my game, I have to decrement the value of them.
var tempo1:Timer = new Timer(4000);
var tParada:Timer = new Timer(2000, 1);
I wonder how can I do to go decrementing these values, starting from an external class ...
Thank U.
Just decriment the delay every time the timer fires.
var tempo1:Timer = new Timer(4000);
tempo1.addEventListener(TimerEvent.TIMER, tick);
var minValue:int = 1000;
tempo1.start();
function tick(e:TimerEvent):void {
if(tempo1.delay - 100 >= minValue){
tempo1.delay -= 100;
}
}
Or, if wanted it smoother, you could do something like this:
import flash.events.TimerEvent;
import flash.utils.Timer;
import fl.transitions.Tween;
import fl.transitions.easing.*;
var tempo1:Timer = new Timer(33); //30 times a seconds or so
tempo1.addEventListener(TimerEvent.TIMER, tick);
var curTickTime:int = 4000;
tempo1.start();
function tick(e:TimerEvent):void {
if(tempo1.delay * tempo1.currentCount >= curTickTime){
trace("tick"); //this should effectively be a tick
tempo1.reset();
tempo1.start();
//do whatever you do on a tick
}
}
//tween the tick delay from the starting value to 100ms over a period of 5 seconds
var tween:Tween = new Tween(this, "curTickTime", Strong.easeOut, curTickTime, 100, 5, true);

Actionscript Loader Error #1009

I'm trying to make a basic loader that uses Splash.swf to then load myisogame.swf
I keep getting error #1009. It's a template we was given in class, originally the Splash.swf directed to Game.swf, but in the code below there is only one mention of where it directs to.
In the template Game.fla doesn't have any code in it.
Here is the Splash
package {
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
public class Splash extends MovieClip {
var myLoader:Loader;
var loadingAnim:LoadingAnimation;
var percentLoaded:TextField =new TextField();
public function Splash() {
// constructor code
myLoader = new Loader();
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
var blubox = new BluBox();
addChild(blubox);
blubox.x=200;
blubox.y=200;
loadingAnim = new LoadingAnimation();
addChild(loadingAnim);
loadingAnim.x=200;
loadingAnim.y=200;
loadingAnim.scaleX=0;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0xFF0000;
format.size = 20;
format.underline = false;
percentLoaded.defaultTextFormat = format;
addChild(percentLoaded);
percentLoaded.x=200;
percentLoaded.y=230;
myLoader.load(new URLRequest("myisogame.swf"));
}
function onProgress(evt:ProgressEvent):void {
var nPercent:Number = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
loadingAnim.scaleX = nPercent / 100;
percentLoaded.text = nPercent.toString() + "%";
trace("load%"+nPercent.toString());
}
function onComplete(evt:Event):void {
myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
loadingAnim.x=1000;
addChild(myLoader);
}
function onIOError(evt:IOErrorEvent):void {
trace("IOError loading SWF");
}
}
}
5
6
7
8
11
12
13
14
20
25
26
MyIsoGame code
package {
import flash.display.MovieClip;
import as3isolib.display.scene.IsoScene;
import as3isolib.display.IsoView;
import as3isolib.display.scene.IsoGrid;
import as3isolib.graphics.Stroke;
import as3isolib.display.primitive.IsoBox;
import as3isolib.display.IsoSprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import eDpLib.events.ProxyEvent;
import as3isolib.geom.Pt;
import as3isolib.geom.IsoMath;
import as3isolib.graphics.SolidColorFill;
// before class
public class MyIsoGame extends MovieClip {
public var scene:IsoScene;
public var view:IsoView ;
public var CELLSIZE:int = 30;//------This is very important to remember cell size. This will affect placement of objects
var zoomFactor:Number = 1;////Zoom Factor heerrrreee--------------------
var s1:IsoSprite = new IsoSprite();
var wr:IsoSprite = new IsoSprite();
var wre:IsoSprite = new IsoSprite();
var g1:IsoSprite = new IsoSprite();
var b1:IsoSprite = new IsoSprite();
var b2:IsoSprite = new IsoSprite();
var b3:IsoSprite = new IsoSprite();
public function MyIsoGame() {
// constructor code
trace("hello from constructor");
scene = new IsoScene();
view = new IsoView();
view.setSize((stage.stageWidth), stage.stageHeight);
view.clipContent = true;
view.showBorder = false;
view.addScene(scene);
view.addEventListener(MouseEvent.MOUSE_DOWN, onStartPan, false, 0, true);
view.addEventListener(MouseEvent.MOUSE_WHEEL, onZoom, false, 0, true);
addChild(view);
var g:IsoGrid = new IsoGrid();
g.addEventListener(MouseEvent.CLICK, gridClick);
g.cellSize=30;
g.setGridSize(6,6);
g.y=0;
g.x=0;
g.gridlines = new Stroke(2,0x666666);
g.showOrigin = false;
scene.addChild(g);
g.addEventListener(MouseEvent.CLICK, grid_mouseHandler);
var box:IsoBox = new IsoBox();
box.setSize(10, 10, 10);
box.moveTo(50,50,0);
scene.addChild(box);
wr.setSize(30, 30, 30);
wr.moveTo(60, 0, 0);
wr.sprites=[new Wrecked()];
scene.addChild(wr);
wre.setSize(30, 30, 30);
wre.moveTo(30, 0, 0);
wre.sprites=[new Wrecked()];
scene.addChild(wre);
g1.setSize(30, 30, 30);
g1.moveTo(150, 150, 0);
g1.sprites=[new Gold()];
scene.addChild(g1);
b1.setSize(30, 30, 30);
b1.moveTo(120, 120, 0);
b1.sprites=[new Blue()];
scene.addChild(b1);
b2.setSize(30, 30, 30);
b2.moveTo(120, 150, 0);
b2.sprites=[new Blue()];
scene.addChild(b2);
b3.setSize(30, 30, 30);
b3.moveTo(150, 120, 0);
b3.sprites=[new Blue()];
scene.addChild(b3);
scene.render();
stage.addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheel);///MOUSE WHEEL CONTROL
stage.addEventListener (KeyboardEvent.KEY_DOWN, keyboarddownlistener)
}
private function gridClick(event:ProxyEvent):void
{
var me:MouseEvent = MouseEvent(event.targetEvent);
var p:Pt = new Pt(me.localX, me.localY);
IsoMath.screenToIso(p);
y
}
private var panPt:Pt;
private function onStartPan(e:MouseEvent):void
{
panPt = new Pt(stage.mouseX, stage.mouseY);
view.removeEventListener(MouseEvent.MOUSE_DOWN, onStartPan);
view.addEventListener(MouseEvent.MOUSE_MOVE, onPan, false, 0, true);
view.addEventListener(MouseEvent.MOUSE_UP, onStopPan, false, 0, true);
}
private function onPan(e:MouseEvent):void
{
view.panBy(panPt.x - stage.mouseX, panPt.y - stage.mouseY);
panPt.x = stage.mouseX;
panPt.y = stage.mouseY;
}
private function boxClick(e:Event)
{
view.centerOnIso(e.target as IsoBox);
}
private function onStopPan(e:MouseEvent):void
{
view.removeEventListener(MouseEvent.MOUSE_MOVE, onPan);
view.removeEventListener(MouseEvent.MOUSE_UP, onStopPan);
view.addEventListener(MouseEvent.MOUSE_DOWN, onStartPan, false, 0, true);
}
private var zoomValue:Number = 1;
private function onZoom(e:MouseEvent):void
{
if(e.delta > 0)
zoomValue += 0.10;
if(e.delta < 0)
zoomValue -= 0.10;
view.currentZoom = zoomValue;
}
///----------------Grid Mouse Handler
public function grid_mouseHandler (evt:ProxyEvent):void
{
var mEvt:MouseEvent = MouseEvent(evt.targetEvent);
var pt:Pt = new Pt(mEvt.localX, mEvt.localY);
IsoMath.screenToIso(pt);
var roundedX:int = int(pt.x)/30;
var roundedY:int= int(pt.y)/30;
trace("transformed point = "+roundedX +","+roundedY);
///Code that allows things to be put down, located here.
var s:IsoSprite= new IsoSprite();
s.sprites=[new Base()];
s.setSize(30, 30, 30);//Varies via Cell size-
s.moveTo(roundedX*30, roundedY*30, 0);
scene.addChild(s);
scene.render();
}///------------------Grid Mouse Handler
public function mouseWheel (event:MouseEvent){
trace("The Delta value isss: " + event.delta);
//Get current view zoom
zoomFactor+=event.delta*0.04;
view.zoom(zoomFactor)
}
public function keyboarddownlistener(e:KeyboardEvent){
{//Screen-Movement Code
if (e.keyCode == 40)
{// The numbers represent the keyboard buttons
trace("Down Arrow")// I've left these trace commands so you can get a better idea of which one is what.-William 22/04/14
view.pan(0,5);
scene.render();
//Down arrow???
}
}
if (e.keyCode == 38)
{
trace("Up Arrow")
view.pan(0,-5);
scene.render();
//Up arrow???
}
if (e.keyCode == 37)
{
trace("Left Arrow")
view.pan(5,0);
scene.render();
//Left arrow???
}
if (e.keyCode == 39)
{
trace("Right Arrow")
view.pan(-5,0);
scene.render();
//Right arrow???
}
/////OBJECT MOVEMENT code- For moving the donut around
///-- I am going to be working on a version of this game where--
///- everything can be controlled via a Keyboard only -William 22/04/14
if (e.keyCode == 65)
{
trace("A Left <--")
//view.x+=15;//Alternate version
s1.moveBy(30,0,0)
scene.render();
//Left?
}
if (e.keyCode == 68)
{
trace("D Right -->")
//view.x-=15;//Alternate version
s1.moveBy(-30,0,0)
scene.render();
//Right?
}
if (e.keyCode == 83)
{
trace("S Down --v")
//view.x-=15;//Alternate version
s1.moveBy(0,30,0)
scene.render();
//Down?
}
if (e.keyCode == 87)
{
trace("W Up --^")
//view.x-=15;//Alternate version
s1.moveBy(0,-30,0)
scene.render();
//Up?
}
}
}
}
I tried the code you supplied (without the numbers at the bottom) and it worked just fine for me.
error #1009 is a runtime error when you use a variable that hasn't been defined yet. I would suggest debugging from Flash Pro (ctrl + shift +enter) and let it show you where it breaks.
In your FLA, you said there is no code, but in the properties panel you do see the class field with "Splash" in it right? that is what links the class to the FLA.
It is possible that the runtime error you are seeing is not in the Splash class but in either BluBox or LoadingAnimation. Do those have class files? If not, take a look in the Library and you'll have two movie clips that are exported for actionScript with those names. Check in them to see if there might be some framescript.

Why the Tween Class don't work inside a for with Flash AS3?

Problem
I'm trying to make a Tween with many Movieclips using the for command, like this:
for(var i:int = 0; i < mcArray.length; i++) {
new Tween(mcArray[i], "x", Regular.easeOut, 0, 100, 1.0, true);
}
But it doesn't works. I've tried change the code as follow down:
var tween:Tween = new Tween(mcArray[i], "x", Regular.easeOut, 0, 100, 1.0, true);
And it doesn't works too.
I can't use the setInterval or Timer because the Movieclips should be synchronized and the it may cause problems.
Is there a way to do this?
AS3 Tween engine, very bad: by design, by performance etc.
If you can't use Greensock library for animation, you could use GTween, It's great tweening engine under MIT Licence. Even without support and continuous improvement, engine is much better than Tween by Adobe.
Working example, after start, if will collect every object in the center of scene:
package {
import com.gskinner.motion.GTween;
import com.gskinner.motion.easing.Sine;
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Point;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setup();
}
private function createDummyObjects(size:uint):Array {
var result:Array = [], i:uint, object:Shape;
for (i; i < size; ++i) {
object = new Shape();
object.graphics.beginFill(Math.random() * 0xFFFFFF);
object.graphics.drawCircle(0, 0, 20);
result[i] = object;
}
return result;
}
private function setup():void {
var objects:Array = createDummyObjects(10);
var i:uint, len:uint = objects.length, item:DisplayObject, middle:Point = new Point(stage.stageWidth >> 1, stage.stageHeight >> 1);
for (i; i < len; ++i) {
item = objects[i];
item.x = Math.random() * stage.stageWidth;
item.y = Math.random() * stage.stageHeight;
addChild(item);
new GTween(item, 1 + Math.random() * 4, {x: middle.x, y: middle.y}, {ease: Sine.easeInOut});
}
}
}
}
I tried your exact code and it works just fine. You need to load your array initially, and I'm not seeing your code for that.
Try this: make two test MovieClips and put 'em on stage with instance names 'boxA' and 'boxR' Then run this code:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var A:Array = new Array();
A.push(boxB); A.push(boxR);
for(var i:int = 0; i < A.length; i++)
{
new Tween(A[i], "x", Regular.easeOut, 0, 100, 1.0, true);
}
Works, no?
If everything in your code is correct and the code is running perfectly fine, and the tweens just aren't working, then this may be caused by Flash's garbage collector killing your tweens before they can finish.
This issue is relatively well known I think. I have run into this issue myself. Here is an article explaining the cause and how to fix it. http://www.scottgmorgan.com/as3-garbage-collection-the-reason-your-tweens-are-ending-early/
Essentially what you have to do is make sure you have a reference to your tween somewhere in your code.
Here is some code you can try to actually see it happening:
var mcs:Array = [];
var tweens:Array = [];
for (var j:int = 0; j < 50; j++) {
for (var i:int = 0; i < 50; i++) {
var mc:MovieClip = new MovieClip ();
mc.graphics.beginFill (0xff0000);
mc.graphics.drawRect (0, 0, 5, 5);
mc.graphics.endFill ();
mc.x = j * 6;
mc.y = i * 6;
mcs.push (mc);
this.addChild (mc);
new Tween (mc, "x", None.easeNone, mc.x, mc.x + 500, 2, true);
//tweens.push (new Tween (mc, "x", None.easeNone, mc.x, mc.x + 500, 2, true));
}
}
In theory every single red square in this code should tween to the right, but only about half do, for me at least. However if you comment out the second last line and uncomment the last line, it should now work because the array holds a reference to each tween.

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!!");
}

How to use hitTestObject on many objects

I can check if one object is hiting another, but what if I have 10 MovieClip objects, and i want to check if any object is hiting ANY object:
package {
import flash.display.MovieClip;
import flashx.textLayout.events.DamageEvent;
import fl.motion.Animator;
import flash.geom.Point;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.geom.ColorTransform;
public class Test extends MovieClip {
private var arrows:Array;
private var timer:Timer;
public function Test() {
init();
}
private function init():void {
timer = new Timer(1000, 6);
timer.addEventListener(TimerEvent.TIMER, timerEvent);
arrows = new Array();
timer.start();
}
private function timerEvent(e:TimerEvent):void{
var arrow:Arrow = new Arrow();
arrow.x = 5;
arrow.y = Math.random() * 200 + 10;
addChild(arrow);
arrow.addEventListener(Event.ENTER_FRAME, onEnterFrame);
arrows.push(arrow);
//trace(555);
}
private function onEnterFrame(e:Event):void{
e.target.x += 4;
if(e.target.x > 400)
{
e.target.transform.colorTransform = new ColorTransform(0, 0, 1, 1, 0, 0, 1, 1);
e.target.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
e.target.addEventListener(Event.ENTER_FRAME, goBack);
}
}
private function goBack(e:Event):void {
e.target.x -= 4;
if(e.target.x < 50)
{
e.target.transform.colorTransform = new ColorTransform(1, 1, 1, 1, 0, 0, 1, 1);
e.target.removeEventListener(Event.ENTER_FRAME, goBack);
e.target.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
}
}
how can i check if any arrow is touching other arrow object?, doesn't matter what object,I need something like hitTestGlobal
At least you can all objects hitting one point by using method DisplayObjectContainer.getObjectsUnderPoint(point:Point). If boundaries of your main object is not changing you can predefine edge points that you will hit testing every EnterFrame event.
Yes. You will have to check hit test on every object you need. And yes, it's a costy operation, but when writing games there's no other workaround. Try using Vector instead of an Array for a little performance boost, as Vector is type dependant array and it uses less memory. You can check the syntax HERE.
You'd instantiate it like this:
private var arrows:Vector.<Arrow> = new Vector.<Arrow>();