coordinate function mouse down in action script 3 - actionscript-3

i will make my hammer(movie clip) following cursor and every time I click the mouse / mouse down, the hammer will come down exposed cursor and return to original position. But i just can make the hammer(movie clip) following cursor. How can I do?
hammer.startDrag(true);
enter image description here

After I received your file here is the new code :
Mouse.hide(); //Hide the mouse cursor
//The stage listen the mouse movement
stage.addEventListener(MouseEvent.MOUSE_MOVE, functionA);
function functionA(evt:MouseEvent){
//On mouse movement, the hammer position will change
hammer.x = mouseX; //If you want you can add an offset (ex : stage.mouseX - 5; )
hammer.y = mouseY;
//That's exactly the same as hammer.startDrag(true); but with something more cool, it's you can add an offset for X/Y axes.
}
//The stage is listening on mouse button down
stage.addEventListener(MouseEvent.MOUSE_DOWN, functionName2);
function functionName2(evt:MouseEvent){
//On mouse button down, we enter the movieclip "hammer" and on the timeline we go to and play (gotoAndPlay :D) the label "HammerDown"
//Now if you double click on the hammer on the stage, you will enter the movieclip and you will find a layer call "Labels"
//I called it like that, the name is not important. In Flash you can add a name to a key frame or a group of keys. Maybe you don't see
//any advantage for now but trust me, that's much more upgradable like that. So in my first answer on Stackoverflow I wrote to you
//to type : gotoAndPlay( 1 ), the number was the key's number, but this time I've change it to "HammerDown" wich is the name of my label
//on the layer labels inside hammer's movieclip.
//So at the end what happen is : When the stage receive a mouse down event, the script enter "hammer" movieclip and gotoAndPlay the animation
//start from the first key of "HammerDown" label. That's what the line below means.
hammer.gotoAndPlay( "HammerDown" ); //2 is the number of the key wich contains the hammer with down position
//To give a label's name to a key or a group of key, just click on a keyframe, go to Propriety -> Label -> Name and that's it!
}
//On mouse button up
stage.addEventListener(MouseEvent.MOUSE_UP, functionName3);
function functionName3(evt:MouseEvent){
//On mouse up the script enter hammer's movieclip and gotoAndPlay the animation inside "hammer" movieclip, wich start from "HammerUp".
//The trick is that the animation will be stop because at the same time, the script read the stop(); and so the animation is stop inside "Hammer".
hammer.gotoAndPlay("HammerUp"); //1 is the number of the key wich contains the hammer with up position
}
And here is your file with my editing :
File

What you can do is to use a movieclip with the hammer. Inside this movieclip you have your timeline, on the first keyn of the timeline you stick the hammer with the "up" position and on this same key you type the following code
stop();
And then on the next key you create a new frame with the hammer down, you also add :
stop();
That will stop the frame reader on the first key and what we are going to do is to instance the movieclip on the stage and make it follow the mouse.
//Instance the hammer
var hammer:Hammer = new Hammer(); //Hammer is the name of you class on the movieclip in your flash library
stage.addChild(hammer); //instance on the stage
//Add an event listener on the stage wich is trigger when the mouse move
stage.addEventListener(MouseEvent.MOUSE_MOVE, functionName);
function functionName2(evt:MouseEvent){
hammer.x = stage.mouseX; //If you want you can add an offset (ex : stage.mouseX - 5; )
hammer.y = stage.mouseY;
}
Now the hammer should be moving and following the mouse :
//On mouse button down
hammer.addEventListener(MouseEvent.MOUSE_DOWN, functionName2);
function functionName2(evt:MouseEvent){
evt.target.goToAndStop(2); //2 is the number of the key wich contains the hammer with down position
}
//On mouse button up
hammer.addEventListener(MouseEvent.MOUSE_UP, functionName3);
function functionName3(evt:MouseEvent){
evt.target.goToAndStop(1); //1 is the number of the key wich contains the hammer with up position
}
The advantage of this technic is that if you feel friendly with Flash animation, you can create a small animation between keys with "goToAndPlay(x)", that's not only "up/down", you can add some fun.
Good luck!

Related

make movie clip with function mouse down in action script 3

i will make my hammer(movie clip) following cursor and every time I click the mouse / mouse down, the hammer will come down exposed cursor and return to original position. But i just can make the hammer(movie clip) following cursor. How can I do?
hammer.startDrag(true);
First you should create an animation that you want hammer performs when clicking. Then execute that animation whenever user mouse downs.
Let's say hammer has an animation and starts at frame #2 then;
private function MainFunction():void
{
hammerMC.addEventListener(MouseEvent.MOUSE_DOWN, CursorDown);
hammerMC.addEventListener(MouseEvent.MOUSE_UP, CursorUp);
//and other required events...
}
private function CursorDown(ref:MouseEvent):void
{
hammerMC.gotoAndPlay(2);//hammerMC.nextFrame();
//...
}
private function CursorUp(ref:MouseEvent):void
{
//we do this because we want cursor comes back to default status after we release the mouse button
//otherwise it sticks to mouse down animation
hammerMC.gotoAndStop(1);
//...
}
I guess this will do the job enough.

AS3 parallax effect in Flash banner preventing Movieclip buttons from functioning

I'm building a rich media Flash banner ad. I have 3 to 4 round MovieClip buttons that move around with an AS3 parallax effect. It looks like an EnterFrame loop creates the effect in AS3. But the individual Movieclip buttons won't function when I assign MouseEvent Click events. I want the parallax movement to stop and playhead advance to a specific label on main timeline when the MC's are clicked.
I'm sure this can be done but I lack the expertise. Here is the code:
//add an event listener to trigger every frame
addEventListener(Event.ENTER_FRAME, onFrame);
//set a constant that marks the centre of the stage
//the stage is 600 x 400, so we halve that
const stageCentre:Point=new Point(180,300);
//set an easing constant
const ease:Number=0.2;
function onFrame(e:Event) {
//create a point to store the distances the mouse is from the centre
var mouseOffset:Vector3D=new Vector3D(stageCentre.x-mouseX,stageCentre.y-mouse Y, 0);
//move each background layer by a different percentage of offset
//the easing constant is used here to create smoother results
//foreground moves the most; 75% of the mouse offset
clip1_mc.x+=(stageCentre.x+mouseOffset.x*0.70 - clip1_mc.x)*ease;
clip1_mc.y+=(stageCentre.y+mouseOffset.y*0.50 - clip1_mc.y)*ease;
//mid-ground moves a medium amount; 50% of the mouse offset
clip2_mc.x+=(stageCentre.x+mouseOffset.x*1.00 - clip2_mc.x)*ease;
clip2_mc.y+=(stageCentre.y+mouseOffset.y*1.00 - clip2_mc.y)*ease;
//background moves the least; 25% of mouse offset
clip3_mc.x+=(stageCentre.x+mouseOffset.x*1.75 - clip3_mc.x)*ease;
clip3_mc.y+=(stageCentre.y+mouseOffset.y*1.00 - clip3_mc.y)*ease;
}
//Click on button to go to and Play "kelsey" label (this does NOT work)
clip1_mc.kelsey_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
MovieClip(root).gotoAndStop("kelsey");
}
Make sure the clip1_mc is on the top most layer, it is probably overlapped by other clips that are catching up the mouse click events.
If you have buttons in all layers you will need to disable mouse events for everything except the buttons. For example, if you have a "button" and a "background" in each movieclip and you want to only keep the buttons clickable, do something like this inside of that movieclip:
background.mouseEnabled = false;
background.mouseChildren = false;
This way the background will not listen for any mouse interactions
add removeEventlistener when the mouse clicks the movieclip
clip1_mc.kelsey_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
this.removeEventListener(Event.ENTER_FRAME, onFrame);
MovieClip(root).gotoAndStop("kelsey");
}

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.

Problems disabling movieclip button flash/as3

I've got a screen which involves a movie-clip where the object has a outline to symbolize that it can be clicked. Upon clicking the object, I'm requesting it to do numerous functions, disable itself and then go to another frame which removes the outline symbolizing it cannot be clicked anymore. But once you disable an object it goes to the original frame.
The object itself consists of these 3 frames.
Frame 1: Original State (Glow)
Frame 2: Hover over giving stats
Frame 3: No glow
To summerise i'd like to click the object and for it to go to the no glow frame and disable the movieclip.
The movieclip enabled = 1 is for when the user returns to the this frame, so the scene is aware of the button press.
Movieclip.addEventListener(MouseEvent.CLICK, Fun_Movieclip);
Movieclip.addEventListener(MouseEvent.MOUSE_OVER, Fun_MovieclipMouseOver);
Movieclip.addEventListener(MouseEvent.MOUSE_OUT, Fun_MovieclipMouseOut);
function Movieclip(event:MouseEvent):void
{
MovieclipEnabled = 1;
Movieclip.gotoAndStop(1);
Movieclip.mouseEnabled = false;
}
function Fun_MovieclipMouseOver(event:MouseEvent):void
{
Movieclip.gotoAndStop(2);
}
function Fun_MovieclipMouseOut(event:MouseEvent):void
{
Movieclip.gotoAndStop(3);
}
For some reason when ever the movieclip is disabled, it always reverts back to the glow state. Does anyone have a solution for this? Cheers
Edit: Inside the movieclip, the first frame has Stop();. Don't know if this would interfere with it.
mc.addEventListener(MouseEvent.CLICK, clickHandler);
mc.addEventListener(MouseEvent.MOUSE_OVER, mouseoverHandler);
mc.addEventListener(MouseEvent.MOUSE_OUT, mouseoutHandler);
function clickHandler(event:MouseEvent):void
{
mc.gotoAndStop(3);
mc.removeEventListener(MouseEvent.CLICK, clickHandler);
mc.removeEventListener(MouseEvent.MOUSE_OVER, mouseoverHandler);
mc.removeEventListener(MouseEvent.MOUSE_OUT, mouseoutHandler);
}
function mouseoverHandler(event:MouseEvent):void
{
mc.gotoAndStop(2);
}
function mouseoutHandler(event:MouseEvent):void
{
mc.gotoAndStop(1);
}
Not entirely sure what you meant by:
The movieclip enabled = 1 is for when the user returns to the this frame, so the scene is aware of the button press.
My suggestion for getting the scene to recognize the button click is to have the scene also listen to the mouse click handler

Adding stop(); to a movieclip on the timeline is causing the tweens in said movieclip to not play, the movieclip just skips to the end

I've got a movieclip that is composed of 3 separate symbols. 2 of the symbols have their alpha tweened over 60 frames. 1 of the symbols is not tweened at all. All symbols are in separate layers, and there is a 4th, empty layer with a keyframe on frame 60 for actionscript.
The actionscript on frame 60 is simply "stop();" .
I am adding an instance of the movieclip to the stage dynamically from the document class. When I have "stop();" in there, the movieclip appears on the stage and skips straight to frame 60, where it succesfully stops.
Without "stop();" in there, the movieclip plays the alpha tweens perfectly, but obviously continuously loops.
Manually dispatching an Event.COMPLETE and listening for it does not work either and I would prefer not doing it that way anyway.
Here is the code that adds the movieclip to the stage:
//initialize the gameplay, remove title screen.
private function initialize_gameplay():void
{
//remove the title screen
initialize_title_screen(true);
this.screen_transition_obj = new tide_out_video();
this.addChild(this.screen_transition_obj);
this.game_board = new tidepool_gameboard();
this.screen_transition_obj.addEventListener(Event.COMPLETE,swap_transition_video_for_screen);
}
//replace the current transition video with the screen it was transitioning to
private function swap_transition_video_for_screen(e:Event){
this.addChild(this.game_board);
if(this.screen_transition_obj != null){
if(this.getChildByName(this.screen_transition_obj.name)){
this.removeChild(this.screen_transition_obj);
}
this.screen_transition_obj.removeEventListener(Event.COMPLETE, swap_transition_video_for_screen);
this.screen_transition_obj = null;
}
}
The movieclip's class is tidepool_gameboard and the property of the document class that stores the reference to it is game_board.
Any idea why putting stop(); on frame 60 of the movie clip is causing it to skip to the end without tweening?
UPDATE:
Adding the movieclip to the stage instantly instead of as the result of an event listener works properly, the problem only occurs when the movieclip is added in the event listner.
I can't believe I overlooked this, as it appears to be fairly obvious to me now.
In the code posted in the question, I've initialized a new instance of this.game_board, then added it to the stage after a delay based on an event listener for a video clip. The animation is playing, but it's playing before the clip ever is added to the stage.
Thanks go to alecmce who answered this question.
I did his Event.ADDED_TO_STAGE event listener, and it worked, which led me to realize that the MovieClip does not wait until it is added to the stage to start playing its own timeline, it simply starts the second you instantiate the object.
This is the new, fully functional code:
//initialize the gameplay, remove title screen.
private function initialize_gameplay():void
{
//remove the title screen
initialize_title_screen(true);
this.screen_transition_obj = new tide_out_video();
this.addChild(this.screen_transition_obj);
this.screen_transition_obj.addEventListener(Event.COMPLETE,swap_transition_video_for_screen);
}
//replace the current transition video with the screen it was transitioning to
private function swap_transition_video_for_screen(e:Event)
{
this.game_board = new tidepool_gameboard();
this.addChild(this.game_board);
if (this.screen_transition_obj != null)
{
if (this.getChildByName(this.screen_transition_obj.name))
{
this.removeChild(this.screen_transition_obj);
}
this.screen_transition_obj.removeEventListener(Event.COMPLETE, swap_transition_video_for_screen);
this.screen_transition_obj = null;
}
}