user7083681
user7083681

Reputation: 53

AS3 Make object visible/invisible depending on position of another object

I'm building a website with a gallery that slides from left to right using AS3 tweening, stopping at specific x coordinates to show a specific item.

When a coordinate is reached, I want to make information about the item visible.

I have this written and I don't understand why it isn't working. When I preview, the Info1 movieclip doesn't appear.

if (GalleryPanel.x==4715.45)
{
    Info1.visible=true;
}
else
{
    Info1.visible=false;
}

This is the code that moves the gallery:

var tweenitem1:Tween = new Tween(GalleryPanel, "x", Strong.easeOut, GalleryPanel.x, 4715.45, 1, true);

For now, Info 1 is just a moviclip I placed on the timeline to test my code. The Info 1 symbol properties were set to invisible; when it is set to visible, it stays visible when GalleryPanel is translated.

Upvotes: 0

Views: 1051

Answers (2)

null
null

Reputation: 5255

Your code doesn't work because it is executed once (and only once) at the beginning. It doesn't continuously monitor the variable.

But doing that would be a bad idea anyway. You'd have to continuously waste resources to check this variable (and if you want to add more destinations it gets even worse).

When a coordinate is reached, I want to make information about the item visible.

The coordinate is reached when the tween finishes. To recognise that, just add a listener for the TweenEvent.MOTION_FINISH. something like

import fl.transitions.TweenEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*;

var tweenitem1:Tween = new Tween(GalleryPanel, "x", Strong.easeOut, GalleryPanel.x, 4715.45, 1, true);

tweenitem1.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);

function onMotionFinish(te:TweenEvent):void
{
    Info1.visible = true;
}

When the next tween starts, to transition to a different state of your application, you should hide Info1 again.

Upvotes: 1

Neal Davis
Neal Davis

Reputation: 2008

Try

if (GalleryPanel.x > 4715 && GalleryPanel.x < 4716)
{
    Info1.visible=true;
}

Using decimal places to check equality is dicey.

Upvotes: 1

Related Questions