Flash Action Script 3: drag movie clip issue - actionscript-3

I am new to as3. I create a rectangle and convert it to symbol. Then I go into the symbol and create two sliders. Also, I make the symbol draggable.
controlPanel_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStartF);
controlPanel_mc.addEventListener(MouseEvent.MOUSE_UP, dragEndF);
function dragStartF(e:MouseEvent){
e.currentTarget.startDrag();
}
function dragEndF(e:MouseEvent){
e.currentTarget.stopDrag();
}
It works fine, but I can't use the slider. If I drag the slider, the entire movie clip moved. How can I solve this problem? Cheers!

Without more info on the structure of your FLA-file I assume that controlPanel_mc is the container containing the rectangle[background] and the two sliders.
What it seems like you did in your code was to add event listener to the container, what that means is that "if a click occures anywhere on this object, do the following". Since that clip "owns" the 2 sliders, the sliders will not get any MOUSE_DOWN-events since the parent is always the one handling the event first.
You probably want to convert the background into a symbol and add the eventListeners to that object instead.
bg.addEventListener(MouseEvent.MOUSE_DOWN, onBgClick);
bg.addEventListener(MouseEvent.MOUSE_UP, onBgRelease);
function onBgClick(e:MouseEvent){
controlPanel_mc.startDrag();
}
function onBgRelease(e:MouseEvent){
controlPanel_mc.stopDrag();
}
That should solve your issue :)

Related

As3 (code snippets) change property of object on stage from within another object

I've been searching for the past few hours how to do this but with no luck. First of all I'm new to AS and not sure what to search for.
So here's what I have and what I want to do:The stage has only 1 frame and on the stage I have 1 button (b1), 1 movie clip that is not visible (area) and 1 visible movie clip that is an animation of 20 frames(ani). For the moment when I press button "b1" it will start the movie "ani" (movie is stopped initially) that will stop when it reaches frame 20. Now what I want is when it reaches the last frame to make movie clip "area" visible. Since I am inside "ani" and on frame 20, I cannot use directly area.visible = true; as I would get the error "Access of unidentified property area." What would be the way to access "area"'s properties from within the other object ?
Inside your "ani" MovieClip (at last frame) add the following sentence,
MovieClip(this.parent).area.visible = true;
Here, parent is your main timeline.
(Note: This approach is not recommended).
Instead, use External classes approach. e.g. use Loader class to load animated swf with COMPLETE event and with contentLoaderInfo get swf object and detect for last frame and make area MovieClip visible.
I'm not sure I completely understand everything you said, but I think what you want to do is something like this on your stage where both ani and area have scope :
ani.addEventListener(Event.ENTER_FRAME, frameCheck);
function frameCheck(e:Event):void
{
if (ani.currentFrame == ani.totalFrames)
{
ani.removeEventListener(Event.ENTER_FRAME, frameCheck);
area.visible = true;
}
}
That was just an example of how you could detect ani hitting the last frame and dealing with that appropriately.
You would need to add the event listener EACH TIME the button is pressed.

ActionScript 3 goto and play event not working

I have a navigation and inside the navigation movie clip, I have buttons and I put this code in my of my button frames
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
this.gotoAndPlay('245');
}
But this didnt work and when I click my button it does nothing, is there something wrong with my code?
this.gotoAndPlay('245');
'245' is a string and gotoAndPlay interprets as a frame label. Remove the quotes, passing an integer, to go to the frame 245.
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
this.gotoAndPlay(245);
}
If that still doesn't work, make sure this refers to the movieclip you want to change the frame. For example, if this code is in the document class, referring to the root instance, this will change the stage's frame.
If you want to change the button frame (only makes sense if it's a SimpleButton instance), change this to aboutbtn, for example.
If it's anything else, you'll to give us more context, it could be a load of other things (different stage? is there a frame 245? is the button mouse enabled? is there any invisible buttons on top of the object?).
Edit:
After clarification: if you want to change the frame of the object above nav, its parent, use:
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
MovieClip(parent).gotoAndPlay(245);
}

dispatchEvent not working in piecemaker 2

i have tried to research this issue, but being a flash newbie i struggle to wade through the terminology...
i am using the piecemaker 2 slider on the landing page of my BudeStrings Dev Site. the slider contains a total of 5 slides; the first is a movie clip containing multiple frames and the following four are animated text clips consisting of only one frame each.
as per instructions i found on the interwebs, i added a keyframe after last frame of the first (multiframe) clip and added the following code:
dispatchEvent(new Event(Event.COMPLETE));
stop();
to stop the clip looping and to enable the autoplay behaviour of the slider. this works absolutely fine with the first (multiframe) movie clip, but when i try to add the same code to the other four (single frame) clips it does not have the desired effect.
these four clips use the following code:
var myString:String = "Sample text "
var myArray:Array = myString.split("");
addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event:Event):void {
if (myArray.length > 0){
TextField.appendText(myArray.shift())­;
}
else {
removeEventListener(Event.ENTER_FRAME, frameLooper);
}
}
to display some text with an animated typing effect. if i add a keyframe with the dispatchEvent code as i did on the first clip it stops working and when i append the dispatchEvent code to the above actionscript it has no effect, and piecemaker 2 stops autoplaying when it hits slide number 2.
beyond that i have no idea what is causing the problem. please help.
regards, jan
First of all, you should use the appendText method on a textfield instance, not the class itself (the compiler should give you an error). So if the textfield on your stage is called 'txt' it would become 'txt.appendText..'. Secondly, you should define the event dispatching within the else section as at the time it runs, your animation would be completed.
[edit] make sure you enable strict mode in your publish settings

Using hitTest instead of mouseOver / mouseOut

Almost all of the Flex components have mouseOver/mouseOut or rollOver/rollOut events. Requires that these events called not the mouse cursor, but on the other element, such as Image or Bitmap.
For example, there is a Colomn Chart and image animations above it and I need to get the data (colomn value or index) when the image is over the column. If it were a component, then I would use hitTest. But what to do if it is chart?
Can't you tell the image/bitmap to not be mouseEnabled?
i can't understand your question properly, eventhough i m trying this...
if you add mouselisteners on the chart and whenever the event is dispatch check..
position.x = event.currentTarget.mouseX;
position.y = event.currentTarget.mouseY;
then you can try with hitTest.
or
if(obj.hitTestPoint(position.x,position.y)){
}
this might be work....

Movieclip stacking in Actionscript

I'm building a game of which the interface is one of the first items to load on screen. Sound button, pause and all the bits -
During the game - all manor of things are dynamically added and removed to the stage. Therefore my interface goes behind my game scene.
How do I ensure the movieclip always stays on top?
Can I override the addChild of my Document Class and every time a new child is added, I restack the interface to the top?
You can use setChildIndex to rearrange objects that are already contained within a MovieClip or Sprite. For example, if you have an object called container, which contains a redBall, blueBall and many other ball objects, you can use the following to make sure redBall is behind everything else:
container.setChildIndex(redBall, 0);
Equally you can make sure that blueBall will be displayed infront of everything else using:
container.setChildIndex(blueBall, container.numChildren-1);
addChildAt will sort you out for adding children straight into their correct position, and setChildIndex will allow you to re-position objects already placed. Hope that helps.
debu
Look into addChildAt, it allows you to specify the index that the new object should be added too.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html#addChildAt%28%29
A good strategy is to keep all interface elements in one parent Sprite/MovieClip, and all game assets in another. (With the interface one on top)
With your guys suggestion I made this, apparently, if you addChild an object already on the screen, it's simply reindex'd to the top. Does this look ok?
private var topLevelChildrenArray:Array = [];
public function addTopLevelChild(child:DisplayObject):DisplayObject
{
topLevelChildrenArray.push(child)
return this.addChildAt( child, this.numChildren - 1 );
}
override public function addChild(child:DisplayObject):DisplayObject
{
this.addChildAt(child, this.numChildren);
for each(var topChild:DisplayObject in topLevelChildrenArray)
{
super.addChild(topChild);
}
return child
}