05-01-2012, 12:04 AM
|
#1
|
|
Registered User
Join Date: Apr 2012
Posts: 4
|
[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 12:11 AM.
Reason: added [as] tags
|
|
|
05-01-2012, 12:54 AM
|
#2
|
|
Registered User
Join Date: Apr 2012
Posts: 4
|
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);
}
|
|
|
05-01-2012, 01:33 AM
|
#3
|
|
Registered User
Join Date: Apr 2012
Posts: 4
|
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
|
|
|
05-01-2012, 02:21 AM
|
#4
|
|
Registered User
Join Date: Apr 2012
Posts: 4
|
Well, whatever was going on, I think TweenLite fixed it.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 10:06 AM.
///
|
|