Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-01-2012, 01:04 AM   #1
negative_antimatter
Registered User
 
Join Date: May 2012
Posts: 4
Default [AS3] AI TweenEvent loop stops after 6.5-7.5 times

I'm making a game where a bat (the white box) bounces back and forth trying to eat the character (the moth). Here's the game as it is now: swfcabin(dot)com/open/1335824567 Arrow keys to move.

The code for the bat checks if it is left or right of the moth, sets the bat's target position, and tweens it there. When the tween is finished, the process is suppose to repeat. This works perfectly 7 and a half times if the moth does not move and 6 and a half times if the moth is constantly in motion. I'm unable to figure out why it wouldn't work indefinitely. (Actually playing the game online just now seems to have the bat bouncing back and forth perfectly or less than one bounce. Now I'm really confused.)

Here's the code relevant to the bat:

ActionScript Code:
var batX = 10; var batY = 10; //This sets the bat's x position to tween to var batXAim:Number = 550 - bat_mc.width/2; bat_mc.x = batX; bat_mc.y = batY; stage.addEventListener(Event.ENTER_FRAME, repeatFlowers); function repeatFlowers(e:Event):void {     //stores location of bat_mc for checkLocation     batX = bat_mc.x;     batY = bat_mc.y;         //keeps bat on screen     if(bat_mc.x < 0 + bat_mc.width/2)     {         bat_mc.x = bat_mc.width/2;     }     if(bat_mc.x > 550 - bat_mc.width/2)     {         bat_mc.x = 550 - bat_mc.width/2;     }     if(bat_mc.y > 400 - bat_mc.height/2)     {         bat_mc.y = 400 - bat_mc.height/2;     }     if(bat_mc.y < 0 + bat_mc.height/2)     {         bat_mc.y = 0 + bat_mc.height/2;     } } stage.addEventListener(Event.ENTER_FRAME, startCheckLocation); function onFinish(e:TweenEvent):void {     stage.addEventListener(Event.ENTER_FRAME, checkLocation); } function checkLocation(e:Event):void {     stage.removeEventListener(Event.ENTER_FRAME, checkLocation);         if(batX > mothLeft_mc.x)     {         batXAim = 0 + bat_mc.width/2;     }     if(batX < mothLeft_mc.x)     {         batXAim = 550 - bat_mc.width/2;     }              var tweenScaleX:Tween = new Tween(bat_mc, "x", None.easeNone,     batX, batXAim, 3, true);     var tweenScaleY:Tween = new Tween(bat_mc, "y", None.easeNone, batY, mothLeft_mc.y, 3, true);         tweenScaleX.addEventListener(TweenEvent.MOTION_FINISH, onFinish); } function startCheckLocation(e:Event):void {     stage.removeEventListener(Event.ENTER_FRAME, startCheckLocation);     stage.addEventListener(Event.ENTER_FRAME, checkLocation); }

Last edited by negative_antimatter; 05-01-2012 at 01:11 AM. Reason: added [as] tags
negative_antimatter is offline   Reply With Quote
Old 05-01-2012, 01:54 AM   #2
negative_antimatter
Registered User
 
Join Date: May 2012
Posts: 4
Default

I've also tried this, but to the same effect.

ActionScript Code:
//starts bat attack looping checkLocation(null); function checkLocation(e:TweenEvent):void {     if(batX > mothLeft_mc.x)     {         batXAim = 0 + bat_mc.width/2;     }     if(batX < mothLeft_mc.x)     {         batXAim = 550 - bat_mc.width/2;     }              var tweenScaleX:Tween = new Tween(bat_mc, "x", None.easeNone, batX, batXAim, 3, true);     var tweenScaleY:Tween = new Tween(bat_mc, "y", None.easeNone, batY, mothLeft_mc.y, 3, true);         tweenScaleX.addEventListener(TweenEvent.MOTION_FINISH, onFinish); } function onFinish(e:TweenEvent):void {     if(batX > mothLeft_mc.x)     {         batXAim = 0 + bat_mc.width/2;     }     if(batX < mothLeft_mc.x)     {         batXAim = 550 - bat_mc.width/2;     }              var tweenScaleX:Tween = new Tween(bat_mc, "x", None.easeNone, batX, batXAim, 3, true);     var tweenScaleY:Tween = new Tween(bat_mc, "y", None.easeNone, batY, mothLeft_mc.y, 3, true);         tweenScaleX.addEventListener(TweenEvent.MOTION_FINISH, checkLocation); }
negative_antimatter is offline   Reply With Quote
Old 05-01-2012, 02:33 AM   #3
negative_antimatter
Registered User
 
Join Date: May 2012
Posts: 4
Default

This post sounds like it may address my problem, but I'm not totally sure what he's talking about: codingjourney. blogspot. com/2008/11/as3-tween-class-randomly-stops-during.html
negative_antimatter is offline   Reply With Quote
Old 05-01-2012, 03:21 AM   #4
negative_antimatter
Registered User
 
Join Date: May 2012
Posts: 4
Default

Well, whatever was going on, I think TweenLite fixed it.
negative_antimatter is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:37 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.