Spawn MC addChild to stage but how to tie in the draggable function? AS3 - actionscript-3

I'm running into some problems with making the movie clip so I can drag it about the stage. The scenario: the user clicks a button that spawns the movie clip to the stage, from there they can move the item around the stage - it's a draggable object. I can make it so the movieClip spawns, I can make it so it drags... but i can't make it do both, once I try to attach the drag function. Any pointers please? I'm new to as3! :-)
spawner_btn.addEventListener(MouseEvent.CLICK, spawnspinkjewel);
function spawnspinkjewel(event:MouseEvent):void
{
var myChild:pink_jewel= new pink_jewel();
stage.addChild(myChild)
myChild.x=300;
myChild.y=150;
}
pink_jewel.addEventListener(MouseEvent.MOUSE_DOWN, pickupblack_pink_jewel);
pink_jewel.addEventListener(MouseEvent.MOUSE_UP, dropblack_pink_jewel);
function pickupblack_pink_jewel(event:MouseEvent):void {
event.target.startDrag(true);
}
function dropblack_pink_jewel(event:MouseEvent):void {
event.target.stopDrag();
}

You added the listeners to the incorrect object.
Change this:
pink_jewel.addEventListener(MouseEvent.MOUSE_DOWN, pickupblack_pink_jewel);
pink_jewel.addEventListener(MouseEvent.MOUSE_UP, dropblack_pink_jewel);
To this:
myChild.addEventListener(MouseEvent.MOUSE_DOWN, pickupblack_pink_jewel);
myChild.addEventListener(MouseEvent.MOUSE_UP, dropblack_pink_jewel);

Related

MouseEvent not dispatching on movie clip

I know this must be easy but somehow I have not been able to figure it out after spending more than hours. I have two movie clips in a class that extends Sprite. When I add event listener to the stage every thing works fine. But when I try to add event listener to one of the movie clips, the event never dispatches it seems. Here is how it looks like -
public class MyClass extends Sprite
{
private var movieclip1:MovieClip, movieclip2:MovieClip;
private function init(e:Event == null):void
{
movieclip1 = new MovieClip();
movieclip2 = new MovieClip();
//works fine, dispatches event
stage.addEventListener(MouseEvent.MOUSE_DOWN. mousedown);
//not working
movieclip1.addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
addChild(movieclip1);
addChild(movieclip2);
}
}
Actually I want both the movie clips to work mutually exclusively, i.e., a mouse event on one movie clip should not interfere with that of the other. Any pointers?
Empty MovieClips and Sprites CANNOT be sized, they will be 0,0 and then they will not be able to dispatch MouseEvents.
You can resize a MovieClip when it has some content.
The Sprite will be of the size of the rectangle that encloses the 2 MovieClip.
If the MovieClips are empty = 0,0 the Sprite will be 0,0
About the events between the 2 MovieClips:
They are not going to interfere because the events when are bubbling goes upwards.
So the listeners put on MC1 will listen ONLY onClick on MC1 and the same does MC2
You can trace the width and height of the movieclip1 or movieclip2, you will get 0 both.
So, you can't click them.
You can use graphcis to draw a shape in these two mcs, and try again.

ActionScript 3 - cannot access MovieClip on stage via root

I have a movieclip called PointerMC. Inside PointerMC is a tween which has actionscript. PointerMC starts off as
pointerMC.visible=false;
. I have a MovieClip called playMC as well on the stage. Here is the AS:
playMC.addEventListener(MouseEvent.CLICK, playClick);
function playClick(evt:Event):void {
pointerMC.gotoAndPlay(1);
}
and here is the AS inside pointerMC:
stop();
MovieClip(root).pointerMC.visible=true;
Now, when I click playMC, I want to make pointerMC visible and then play the tween inside PointerMC, I know I can do
function playClick(evt:Event):void {
pointerMC.visible=true;
pointerMC.gotoAndPlay(1);
}
but how do I make pointerMC visible while I am inside the MovieClip? How come
MovieClip(root).pointerMC.visible=true;
is not making PointerMC visible?
If you're already inside of pointerMC, then you shouldn't have to use root to access it, you can just use the this keyword: this.visible = true;

Clicking on a movieclip inside another movieclip on AS3

Alright, what i need it's simple but its driving me crazy, i want to know if AS3 detects my mouse inside a movieclip.
For example, i have a movieclip instanced "BEframes" which is inside movieclip "BE1" and i want to put him inside a new movieclip instanced "roll". So the order would be roll > BE1 > BEframes.
I want to know if flash will only detect "roll" or he will detect all movieclips, thank you,
for(i=1;i<=77;i++){
var str:String =("BE" + i);
this[str].BEframes.gotoAndStop(i);
this[str].addEventListener(MouseEvent.CLICK, clique);
this[str].addEventListener(MouseEvent.ROLL_OVER, over);
this[str].addEventListener(MouseEvent.ROLL_OUT, out);
}
function clique(evt:MouseEvent):void{
var botao:String = evt.currentTarget.name.toString();
var num:String = botao.replace("BE", "");
parede_esquerda.gotoAndStop(num);
}
function out(evt:MouseEvent):void {
evt.currentTarget.gotoAndPlay("out");
}`enter code here`
function over(evt:MouseEvent):void {
evt.currentTarget.gotoAndPlay("over");
}
*
Probably, you should use MOUSE_OVER and MOUSE_OUT instead of ROLL_OVER and ROLL_OUT.
this[str].addEventListener(MouseEvent.MOUSE_OVER, over);
this[str].addEventListener(MouseEvent.MOUSE_OUT, out);
To avoid receiving mouseEvent for movieClips set mouseEnabled to false, i.e if you don't want clip roll's mouse event setroll.mouseEnabled = false so that the object below will receive mouseEvent

Changing individual movieclip's frame rate with TweenMax

My aim is to change the frame rates of my individual (looping) movie clips through clickable controls (slow/med/fast). I've heard it isnt possible to achieve this through as3/flash alone, so I've tried greensock's TweenMax... However I can't seem to figure out how to do this. Is there anyone that could help?
box1.addEventListener(MouseEvent.MOUSE_DOWN, box1down);
function box1down(event:MouseEvent):void {
//FRAMERATE CODE HERE
}
Many thanks!
Here is the API doc for TweenMax: http://www.greensock.com/as/docs/tween/com/greensock/TweenMax.html
If you have multiple movieclips that you are trying to control, you can just create an abstract class with the functionality you want and extend that class. So something like:
public class ControlledMovieClip extends MovieClip {
public function ControlledMovieClip() {
stop();
}
public function animate(frameRateInSeconds:Number):void {
TweenMax.to(this, frameRateInSeconds, { frame: this.totalFrames - 1, repeat: -1, ease: Linear.easeNone });
}
}
Have all your movieclips that are looping extend that class, and then you could call the animate function on the objects in your box1down event handler.
I haven't tested that code so you might need a gotoAndStop(1) at the end of each iteration.
It's possible through Actionscript alone it just requires you to handle the frame progression yourself (instead of using mc.play() you stop the movieclip and call nextFrame() yourself).
Lets say a Movieclip (myMC) has 20 frames of animation. To manually run the animation at normal speed you simply call myMC.nextFrame(); on every frame of your project (using an ENTER_FRAME listener for example).
To have the animation run at half speed you can use a frame count and a frame trigger:
var frameTick = 0;
var frameAnimTrigger = 2;
function Update(e:Event):void
{
frameTick++;
if(frameTick == frameAnimTrigger)
{
myMC.nextFrame();
frameTick = 0;
}
}
Because nextFrame is only called every other frame the animation appears to run at half speed.

Flash drag strange behavior

I have a fancy movie clip (with text and other movie clips inside it)
that I want to drag
I can only drag internal parts of the movie clip but not the whole movie clip.
My NOT working example:
http://www.internetmotors.lv/temp/testgame_5.swf
http://www.internetmotors.lv/temp/testgame_5.fla (cs5)
Code:
drag_1.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
function clickToDrag(event:MouseEvent):void
{
event.target.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
function releaseToDrop(event:MouseEvent):void
{
event.target.stopDrag();
}
answering my own question:
it seems i need to use
event.currentTarget.startDrag();
instead of:
event.target.startDrag();