Skip to main content
Participant
June 6, 2014
Answered

as3 tween bug

  • June 6, 2014
  • 1 reply
  • 493 views

Greetings fellow explores, = )

I've been at this for quite some time now and have sadly met my match. for the sake brevity ill be quick. i have a document class that runs a timer and programatically loads a movieclip and a for loop that stacks them one on top of the other. then a dynamic tween move them from one end of the stage to the other. here is the issue.When i run the swf occasionally one or two tweens stop mid-way and i havent a clue as to why that is.

document file

package {

  //are main stage is a movieclip

  import flash.display.MovieClip;

  import flash.events.Event;

  import flash.utils.Timer;

  import flash.utils.*;

  import flash.events.*;

  import fl.transitions.Tween;

  import fl.transitions.easing.*;

  import fl.transitions.easing.None;

  import fl.transitions.TweenEvent;

  public class Game extends MovieClip {

  //varibles

  public var brickStack1:Array = new Array(1);

  public var brickStack2:Array = new Array(1,1);

  public var brickStack3:Array = new Array(1,1,1);

  public var brickStack4:Array = new Array(1,1,1,1);

  public var brickStack5:Array = new Array(1,1,1,1,1);

  public var arrayOfBricks:Array = new Array(brickStack1,brickStack2,brickStack3,brickStack4,brickStack5);

  public var brickLoader:Timer = new Timer(2000,0);

  //public var arrayLength:int = 0;

  public var brickStack:int = 0;

     public var brickStackBottom:int = 0;

  var arrayLength:int = 0;

  var arrayLengthBottom:int = 0;

  var b:int = 0;

  public function Game() {

  trace("game");

  //addEventListener (TimerEvent.TIMER , loop);

  brickLoader.addEventListener (TimerEvent.TIMER , loop);

  brickLoader.start();

  //create a new instance of the Brick

  //var brickTween = new Tween( k,"x", None.easeNone , k.x,k.x - 1000, 5, true);

  }

  function loop(e:TimerEvent):void {

  //declare a varible for the for loop with Math.random

  brickStack = Math.floor(Math.random() * 5) ;

  //trace(brickStack);

  if(brickStack == 0) {

  brickStackBottom = 4;

  }

  if(brickStack == 1) {

  brickStackBottom = 3;

  }

  if(brickStack == 2) {

  brickStackBottom = 2;

  }

  if(brickStack == 3) {

  brickStackBottom = 1;

  }

  if(brickStack == 4) {

  brickStackBottom = 0;

  }

  arrayLength = arrayOfBricks[brickStack].length;

  arrayLengthBottom = arrayOfBricks[brickStackBottom].length;

  trace(arrayLengthBottom);

  //arrayOfBricks[brickStack];

  //brick load ratio 1:5       2:4      3:3      4:2      5:1

                   //+4*5/5   +2*4/4   +0*3/3   -2*2/2   -4*1/1

  //formulate a mathmatically loader paradiam

  for(var i:int = 0;i < arrayLength; i++){

  bottomLoop();

  

  var obj:Brick = new Brick();

  obj.x = stage.stageWidth + obj.width;

  obj.y = (i*40);

  addChild(obj);

  var brickTween = new Tween( obj,"x", None.easeNone , obj.x,obj.x - (stage.stageWidth + obj.width*2), 4, true);

  }

  

  b = 0;

  //k = someArray[]

  //

  }

  function bottomLoop() {

  for(var o:int = 0;o < arrayLengthBottom; o++){

  var objBottom:Brick = new Brick();

  objBottom.x = stage.stageWidth + objBottom.width;

  objBottom.y = (stage.stageHeight - 40) - (o*40);

  addChild(objBottom);

  

    var brickTweenBottom = new Tween( objBottom,"x", None.easeNone , objBottom.x,objBottom.x - (stage.stageWidth + objBottom.width*2), 4, true);

  }

  }

  }

}

Brick class

 

package {

  //import necessary flash files/libraries

  import flash.display. *;

  import flash.display.MovieClip;

  import flash.events.*;

  //import flash.transition.Tween;

  //import flash.transition.easing.*;

  //import flash.utils.Timer;

  //import fl.transitions.Tween;

  /*import fl.transitions.easing.*;

  import fl.transitions.Tween;

  import fl.transitions.easing.None;

  import fl.transitions.TweenEvent;

  */

  public class Brick extends MovieClip {

  //declare varibles

  //private var brickStack:Number = Math.floor(Math.random()* 5) + 1;//somenumber) + 1

  //private var brickLoader:Timer = new Timer(2000,0);

  //private var arrayOfBricks:Array = new Array(1,2,3,4,5);

  public var _root = MovieClip(root);

  //declare constuctor function no perameters

  //reminder inoder for constuctor function to work Brick sprite must be loaded onto stage

  public function Brick(){

  //tells class what proporties it has

  //brickLoader.addEventListener (TimerEvent.TIMER , brickTimer);

  //brickLoader.start();

  //var brickTween = new Tween(_root.Brick,"x", None.easeNone ,_root.Brick.x, _root.Brick.x - 1000, 5, true);

    

  }

  //function brickTimer(e:TimerEvent): void {

  //trace("brickTimer");

  //}

  }

}

would have place this code in the Brick class but i do not know how reference Brick within its class's source code

would greatly appreciate any assistance .Thank you so very much

This topic has been closed for replies.
Correct answer kglad

Sorry i have but no such luck. could it be that im not removing the children after tween..im at los at and it does me no justice that ive only 2months experience. hope im not in over my head.

var brickTween:Tween;

  function loop(e:TimerEvent):void {

  //declare a varible for the for loop with Math.random

  brickStack = Math.floor(Math.random() * 5) ;

  if(brickStack == 0) {

  brickStackBottom = 4;

  }

  if(brickStack == 1) {

  brickStackBottom = 3;

  }

  if(brickStack == 2) {

  brickStackBottom = 2;

  }

  if(brickStack == 3) {

  brickStackBottom = 1;

  }

  if(brickStack == 4) {

  brickStackBottom = 0;

  }

  arrayLength = arrayOfBricks[brickStack].length;

  arrayLengthBottom = arrayOfBricks[brickStackBottom].length;

  for(var i:int = 0;i < arrayLength; i++){

  bottomLoop();

  

  var obj:Brick = new Brick();

  obj.x = stage.stageWidth + obj.width;

  obj.y = (i*40);

  addChild(obj);

  brickTween = new Tween( obj,"x", None.easeNone , obj.x,obj.x - (stage.stageWidth + obj.width*3), 4, true);

   

  }

  }

  function bottomLoop() {

  for(var o:int = 0;o < arrayLengthBottom; o++){

  var objBottom:Brick = new Brick();

  objBottom.x = stage.stageWidth + objBottom.width;

  objBottom.y = (stage.stageHeight - 40) - (o*40);

  addChild(objBottom);

  

    brickTween = new Tween( objBottom,"x", None.easeNone , objBottom.x,objBottom.x - (stage.stageWidth + objBottom.width*3), 4, true);

  

  }

  }


that's exactly the same issue: you're overwriting brickTween so all but the last can be gc'd before it completes.

use an array to prevent your tweens from being gc'd.  assign the array's length to zero when you want the tweens gc'd:

for (var i: int = 0; i < arrayLength; i++) {
bottomLoop();
var obj: Brick = new Brick();
obj.x = stage.stageWidth + obj.width;
obj.y = (i * 40);
addChild(obj);
brickTweenA.push(new Tween(obj, "x", None.easeNone, obj.x, obj.x - (stage.stageWidth + obj.width * 3), 4, true));

    }

}

function bottomLoop() {

    for (var o: int = 0; o < arrayLengthBottom; o++) {

var objBottom: Brick = new Brick();
objBottom.x = stage.stageWidth + objBottom.width;
objBottom.y = (stage.stageHeight - 40) - (o * 40);
addChild(objBottom);
brickTweenA.push(new Tween(objBottom, "x", None.easeNone, objBottom.x, objBottom.x - (stage.stageWidth + objBottom.width * 3), 4, true));

    }

}

1 reply

kglad
Community Expert
Community Expert
June 6, 2014

that's usually caused by making a tween local to a function so the tween can be gc'd before it completes. eg,

function loop(e:TimerEvent):void{

  var brickTween = new Tween( obj,"x", None.easeNone , obj.x,obj.x - (stage.stageWidth + obj.width*2), 4, true);

}

which should be:

var brickTween:Tween;

function loop(e:TimerEvent):void{

  brickTween = new Tween( obj,"x", None.easeNone , obj.x,obj.x - (stage.stageWidth + obj.width*2), 4, true);

}

Participant
June 6, 2014

thank you , im very grateful  for your shared knowledge , unfortunately it was not the solution to this particular bug.there must be other reasons why the tweens occasionally stops. wish i had the answer or even reading material to point me in the right direction.

kglad
Community Expert
Community Expert
June 7, 2014

copy and paste your corrected code.