What is this, and why is it happening? - actionscript-3

So I have been working on my final project for the semester for my Computer Systems class, and we have been tasked to make a game using Flash and ActionScript 3.0. I've pretty much completed everything but I have come across an extremely strange occurrence of Flash CS6 being silly. I am moving a MovieClip up and down depending on a selected index, however, the image leaves 'residue of it's footprints' behind and also moves. I have tried hard to look for an answer as to why this is happening, but I don't know what it's called, or how to appropriately explain it - I'm coming from an area where one must program graphics, not just simply, drag and drop.
Below are pictures as to what I've come across, but first the code I'm using:
function updateThemeScreen():void {
button_selection.y += (selectedPositions[selectedTheme] - button_selection.y) / 2;
}
function attemptThemeChange(mxP:Number, myP:Number):void {
if(objectContains(theme_darkness, mxP, myP)) {
selectedTheme = 0;
} else if(objectContains(theme_halloween, mxP, myP)) {
selectedTheme = 1;
}
}
As you can see in the final image, it has copied half of itself and left it at the last button, which is strange, and shouldn't happen...
Link to the SWF zipped up with the required AS3 classes: Dude, RUN

So it seems that I have magically fixed this problem by hiding the buttons and showing them all in one frame. I don't know what this problem is or why it does it, but to fix it, you just need to hide and show the affected components - yes, in one frame:
function hideShow(object:MovieClip):void {
object.visible = false;
object.visible = true;
}

Related

AS3 Swap depths in Flash IDE without destroying movieclips

I have two movieclips, one is on top, one is on bottom. The top one is set to the second frame. I move it to the z axis bottom on the next frame, and for the remaining frames it plays all frames like it has been reset without any code (like I didn't tell it gotoAndStop(2)).
Image example: http://i.imgur.com/mQ8f5uT.gif (hard to tell by the frame rate, but the dark green box starts playing as if I didn't set it's frame once I move it below)
I know it doesn't have that issue when I code the depth swapping with setChildIndex, but the animations are lost.
If you're wondering, it's a simplified issue of a larger moving character sprite animation that I would like to swap depths of movie clips in the animation without resetting the movie clips underneath the depth change. So I'm not really looking for a work around per se, unless there's a well design solution and not a band aid "hack."
I think I didn't understand your question fully. A MovieClip will keep playing or keep being stopped no matter what you do with it, even if it was removed from the scene. I made a simple example which shows that MovieClip state is not affected by depth change.
The working SWF is here to test: http://zdg.ru/tmp/updown.swf
And the code is:
import flash.events.MouseEvent;
var is_playing:Boolean = true;
btn_toggle.addEventListener(MouseEvent.CLICK, doToggle);
btn_updown.addEventListener(MouseEvent.CLICK, doUpdown);
function doToggle(evt:MouseEvent) {
if (is_playing) mv_test.stop(); else mv_test.play();
is_playing = !is_playing;
}
function doUpdown(evt:MouseEvent) {
var tmp:int = getChildIndex(mv_block);
setChildIndex(mv_block, getChildIndex(mv_test));
setChildIndex(mv_test, tmp);
}

How to let an object appear in actionscript

I have an animation that may stop at 2 points depending on what the user fills in, if the animation stops at one of the two frames an objects has to apear and have to disappear if the user continues with playing the animation again. Can someone tell me how I can let object appear if the animation stops at a certain frame?
Do I need something like this? I have very little experience so please help!
star_mc._alpha = 0; star_mc.onEnterFrame = function(){ if(this._alpha < 100){ this._alpha = this._alpha + 5; }}
The code looks ok.... try it... just put it in the right place!
It's hard to tell where because I don't know how your animation built!!

Making a movie clip come from other side when leaving frame

So im trying to make an easy script, which will hold a video...
And i will be able to drag the video around as i want, but the catch is... when it leaves borders of the frame, i want it to continue playing on the other side, even if its only half of the video or less...
I know how to transport it, to the other side, but not the half of it or less..
function everyFrame(event:Event):void {
if (movie.x + 100 < 1)
movie.x = 400;
}
Here is an image of the effect that im trying to achieve... http://i.imgur.com/w8E4s.png
Thank you in advance
Without seeing more code, I would say you need to set clipAndEnableScrolling.
clipAndEnableScrolling="true"
Try adding that to the group (Group, HGroup, or VGroup) that you have your video in.
Edit based on me understanding now...:
When the right edge of your movie touches the right edge of the screen, you will need to create a second movie and add it to the stage at a determined x of the first movie and y of first movie. I would say something like this...
onMoveOfFirstMovie():void
{
if(firstMovie.x + firstMovie.width > movieContainer.width)
{
if(secondMovie doesnt exist)
{
//create second movie if it isnt already on stage and set it's play position to the play position of first movie
}
secondMovie.y = firstMovie.y;
secondMovie.x = firstMovie.x + firstMovie.width - movieContainer.width;
}
}
Very rough code but it should be about right.

How to know if a popup is already showing up on the screen in flex

I need to know if a popup (which is a singleton titlewindow, hence would be initialized only once) is already shown in the screen or not, if not then i will call a function to show it up, otherwise do something else.
I tried findPopup.focusEnabled //findPopup is the titlewindow class for popup
But it is always true irrespective of whether the popup is shown in the screen currently or not.
Thanks.
Use isPopUp
Set to true by the PopUpManager to indicate that component has been
popped up.
All objects rendered on the screen have a root:DisplayObject property. If it is removed from the screen, then root will be null. If your concern is whether the pop-up is in front of everything else, then use popUpObj.parent.setChildIndex(popUpObj, popUpObj.parent.numChildren - 1) to ensure it (more on this below). You will need to iterate through all of the parent until you reach the root itself. (With the PopUpManager, I believe that the MovieClip is added directly to the root, so it should only be once, but I don't recall at the moment)
Everything else is obvious:
is alpha = 1?
visible = true?
is width > 5
is height > 5
... I could continue, but I think the idea is pretty clear.
On testing visibility of obscured objects:
Honestly, this is easiest to do on the root.
var firstParent:DisplayObjectContainer = /*
find the ancestor which is on
the root. You may need to look up
"path to root AS3"
*/
var num:int = root.numChildren;
//iterate backwards to exclude things below the target clip.
for( var i:int = num - 1; i >= 0; i-- )
{
var kid:DisplayObject = root.getChildAt( i );
if( kid == firstParent ) break; // no sense in continuing.
if( firstParent.hitTestObject( kid ) )
{
// kid at least partially obscures the pop-up. Do something with it.
}
}
You can check if the object exists with an if block
if(findPopup)
findPopUp.visible=true;
assuming you turned of the visibility off to hide the window. You can of course destroy the object and recreate it if desired. In this case, you don't need that if block because it'll be reconstructed from scratch.
You can still use that if logic to be sure if somewhere in your code, something has already created your popup object. Maybe another class will be doing that behind the scenes so the basic idea is to guarantee that such object exists or not.
[Edit:]
Didn't know you were using PopUpManager. Please use Ranhiru Cooray's answer.
A reminder that PopUpManager has it's own method to put a pop up in front of all other objects: bringToFront(popup)
[/Edit]
Are you trying to find out if the pop up has been added to the stage or if it is still actually visible on the screen and not hidden behind other display objects ?
If it's the first, you could find out with a search (flex 3)
// popUpContainer is the object containing the pop up
if (popUpContainer.contains(FindPopup.getInstance()))
{
// The popup was added to stage
}
else
{
popUpContainer.addChild(FindPopup.getInstance());
}
If it's the second, you could make sure it is visible by adding it to the application root and making sure it has the highest index. But it's hard to test if it's actually being shown on the screen, since it could be 40% or 60% hidden behind other objects.
// Placing the pop up on top of other display objects in the application root
this.setChildIndex(FindPopup.getInstance(), this.numChildren - 1);

Clicking Text Element in Tween Halts Animation

I've been having some trouble with tweens. Here's a description of my usage:
I have a system where a textbox is the child of a movieclip. When you click the "Next" button, the movieclip fades to 0-alpha and upon completion, the text in the textbox is changed (to the next index in an array) and it tweens back in to 100-alpha. This makes a nice transition through the text.
My issue is that sometimes it doesn't tween back in, only out, leaving the user with an empty box where text should be.
However, I'd asked this question previously with the thought that it was "Timing out". Now, after significant testing I realised that it only happens if I click or select some of the text on the text box. Could it have something to do with this text selection intefering with the changeText function below... (it's the same text box, just the text changes).
Has anyone else experienced similar faults?
CODE:
function changeClick(e:MouseEvent):void {
if (e.currentTarget==btnRight) {
newDirect="right";
} else {
newDirect="left";
}
if (newDirect=="right") {
if (pageTotal!=pageCurrent) {
tweenText=new Tween(b_textB,"alpha",Strong.easeOut,1,0,.5,true);
tweenText.addEventListener(TweenEvent.MOTION_FINISH, changeText);
}
} else {
if (pageCurrent!=1) {
tweenText=new Tween(b_textB,"alpha",Strong.easeOut,1,0,.5,true);
tweenText.addEventListener(TweenEvent.MOTION_FINISH, changeText);
}
}
}
function changeText(e:TweenEvent):void {
var newText:String;
var pageCurrentConstant:int=pageCurrent;
if (newDirect=="right") {
for (var i=0; i<=(pageTotal-1); i++) {
if ((pageCurrentConstant-1)==i) {
if (i!=pageTotal-1) {
newText=pageText[i+1];
pageCurrent++;
} else {
newText=pageText[i];
}
}
}
} else {
for (var j=0; j<=pageTotal; j++) {
if (pageCurrentConstant==j) {
if (j!=0) {
newText=pageText[j-2];
pageCurrent--;
} else {
newText=pageText[j];
}
}
}
}
b_textB.htmlText=newText;
tweenText=new Tween(b_textB,"alpha",Strong.easeOut,0,1,.5,true);
drawWidget();
}
changeClick is initiated by either btnRight or btnLeft to navigate through the text
Try disabling the text selection, with b_textB.selectable = false
You will be able to quickly rule out the possibility of a selection issue. But the sometimes in your question strongly indicates that's what the issue is.
If you need the text to be selectable when it's visible, just switch it off and on at the start and end of the tweens.
Hope this solves it.
Oh by the way, here's a list of several completely free alternatives to the Tween class... (Greensock's Tween packages are not free.)
Between AS3
Desuade Motion ... This one in particular is awesome.
Tweener
Tweensy
Twease
Update...
The only way you can solve this and allow the user to select the text, is to make a duplicate textfield that's selectable, and toggle visible off for this when the tween begins and on again when it ends, the alpha property on the effected textfield will then work properly.
Pretty kludgy I know, but it will get the effect to work, and allow the user to select the text when it's visible.
You may also try to wrap the original textfield in a Sprite and do the alpha Tween on that instead, however I don't guarantee that will be a 100% fix.
The standard Tween class is kind of stupid in many situations. When using it, you have to be careful that you don't overwrite or remove its instance, as the Garbage Collection could start then. In the same way it always requires you to specify a start value which can lead to disrupting behaviour in the animation.
I'm not exactly sure what exactly your problem is, and with that less code it is hard to reproduce it (you might want to provide a full working example code if you still experience the problem). However I suggest you to try out a different tweening framework. I for myself have made very good experience with Greensock's TweenLite. It might have a weird syntax (at least I could imagine a better one), but in general it works really well, and I have solved very many problems with the standard Tween class by simply using TweenLite instead.