using mouse_over when dragging a movie clip onto another movie clip in AS3 - actionscript-3

I'm' trying to do the following: I want to drag a mc (mc1) over another mc (mc2), and trigger a timeline navigation when it's over--not when it's releasaed (mouse is up). Apparently flash doesn't recognize the mouse is over mc2 because it's reading it only as being over mc1. This is probably very simple but I'm lost as to how to do it.
Feeble attempt at code:
mc2.addEventListener(MouseEvent.MOUSE_OVER, mcOver1);
function mcOver1(Event:MouseEvent):void {
mc2.gotoAndStop(2);
}
When I'm not dragging anything it works fine, because flash can detect where the mouse is. But I'm unsure how to get flash to detect the mouse's location when it's dragging a movie clip. I tried basing the code on the location of the movie clip I was dragging (mc1) but that didn't work, either.
Any help is appreciated.
EDIT: ok, I just thought of a really cheap workaround, but I'd still like to know how to do it without needing a workaround. Basically, since my mc (mc1) is so small, I made it so that the mc's x-position is 5 pixels more than the cursor's x-position, so mc2 recognizes the mouse now:
this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging1, true);
this.addEventListener(MouseEvent.MOUSE_UP, stopDragging1, true);
function startDragging1(e:MouseEvent) {
mc1.startDrag();
mc1.x = mouseX + 5;
}

Related

How to make a movieclip play on a specific frame in AS3?

I'm incredibly rusty at Flash having not touched it in probably 10 years and can't seem to figure this out, or find it online:
I have a MovieClip with two layers, each having a Shape Tween. Basically its a Door that opens and closes.
I dropped it onto the main timeline but now I need it to start and stop. This is where I'm now struggling since the last time I used Flash actions could go on specific keyframes.
I made a new layer called actions just to keep things organized and currently have:
barrier1.stop();
I just want something that lets me state a frame, say 57 to have barrier1 start playing on. Tried using play(); and Event.ENTER_FRAME with no luck. How would I set this up?
Well it is easy with the instance name of your movieClip
barrier1.stop(); // Stops the movieClip
barrier1.play(); // Resumes
barrier1.gotoAndStop(12) // Goes to 12nd frame and stop
barrier1.gotoAndPlay(12) // Goes to 12nd frame and play
barrier1.currentFrame // returns barrier currentframe
For capturing frame from scene level:
this.addEventListener(Event.ENTER_FRAME,onLoop);
function onLoop(event:Event){
if(barrier1.currentFrame == 57){
trace("BARRIER is in 57. frame");
}
}
Inside on the animation clip on the first frame
var root:MovieClip = this.parent as MovieClip
root.makeStartSceneAnimation()
**in timeline scene level [root]**
function makeStartSceneAnimation(){
/// barrier started to play
}
If you are using timeline, you can add Key frame on the desired frame, and then add stop(); as Action in the action layer. But bear in mind that if you do this in the main timeline - it will stop everything. If you want to stop that MovieClip, then you have to do this inside MoviceClip's timeline.

Movie Clips frozen but swf has not crashed - AS3 CS6

I'm making a top down shooter for a college project. I'm getting extremely low on available time left to complete this.
I followed a tutorial as we was not taught any AS3 although expected to creating a game in this language so there is commented code due to this. The tutorial lead me up to creating a player on the stage that moves by keyboard presses and can fire bullets by pressing the left mouse button, but it didn't help with creating enemies at all.
So I created an enemy movie clip that just spawns on the stage at X=0 and Y=0 (random spawning will be added later), and it's supposed to move towards the players location but it doesn't. When the swf loads up it appears they have started to move but have frozen on the screen. It's not crashed because I can still move the player around and fire bullets? Any help is appreciated.
Main.as code
Enemy.as code
Well, you are not calling the moveTowards() in any loop -> the position is not updated. Or where exactly are you updating the enemy's position? I see only one setting of .x, .y of enemy and it is right after you create the object, no further updates.
Put this into your loop function in Main.as:
for(var i:int=0; i<enemies.length; i++) {
Enemy(enemies[i]).moveTowards(player.x, player.y);
}

Play movie clip on click at mouse coordinates

I'm trying to play a movie clip of a circle expanding and fading away every time a user clicks. I make the movie clip, then I convert it to a movie clip again and create a motion tween making the circle get larger and fade away. But, when I call on the clip it just keeps playing over and over in the last place you click. If I set a stop at the last frame of the tween the next time you click it, it won't play.
fs15secTapBtn.addEventListener(MouseEvent.MOUSE_DOWN, fs15secdownHandler);
function fs15secdownHandler(event:MouseEvent):void
{
circletouch.x = mouseX;
circletouch.y = mouseY;
}
Thanks!
You need to start/stop the movieclip every time you click. Right now, when you click the first time the clip would be put on stage and playing. Then when you click again, all you're doing is modifying the x and y positions of the clip, which moves the clip to the mouse coords...but the clip itself would still be continuing to be in whatever state it is in during the click (playing on it's own at whatever frame).
Within your click handler add in a circletouch.gotoAndPlay(1); to restart the playing at frame 1. Also, you may want to re-add that stop(); back in so that it'll only play once.

AS3 move object back to first frame from current position in tween

I have an object with an instance name of arrow that moves across the page on a motion tweet when a button is pressed. When the button is released I want the arrow to move back to the start of the tween frame by frame.
I have set up an if statement when the button is released and its current frame is more than 1 to go back 1 frame.
So far the arrow is ignoring the if and stopping due to the else statement.
btn.addEventListener(MouseEvent.MOUSE_OUT, out);
function out(e:MouseEvent){
if(arrow.currentFrame > 1){
arrow.gotoAndStop(arrow.currentFrame - 1);
}else{
stop();
}
}
You would probably be better off making another tween on the timeline that goes backwards then just playing that. Another option is to use a third party tweening library like tweenlite since in there you can easily setup animations to run forward or in reverse.
Are you sure arrow is the name of the movie clip that you want to run the frames backwards on, or is that the instance of the element that changes over some other movie-clips timeline?

Removing Sounds Invoked within a MovieClip from main Time Line

I currently have a difficulty removing specific sounds from playing when I advance through the timeline on a particular project.
The user chooses a particular item on the timeline which will display a specific movieclip and will then play a specific sound on MOUSE_DOWN.
The eventListener for MOUSE_DOWN which exists as follows:
stage.addEventListener(MouseEvent.MOUSE_DOWN, sprayWater);
stage.addEventListener(MouseEvent.MOUSE_UP, stopWater);
function sprayWater(event:MouseEvent):void
{
waterarm.gotoAndStop(2);
trace("SPRAYING WATER");
}
function stopWater(event:MouseEvent):void
{
waterarm.water.gotoAndPlay("waterE");
}
on frame 2 of 'waterarm' is a movieclip called 'water' that contains an animation of water and the following code to start the water sound:
var sfxWater:sfxwater;
var waterChannel:SoundChannel;
sfxWater = new sfxwater;
waterChannel = sfxWater.play();
on the frame "waterE" an animation of the water disappearing exists and the code sfxWater.stop();.
When the user progresses beyond this frame on the root timeline, the sound effect of water still remains on MOUSE_DOWN despite the movieclip no longer existing on the timeline at that point.
The ideal outcome will be the individual sound playing on MOUSE_DOWN and stopping on MOUSE_UP only when this movieclip is visible on the main timeline. If anybody can provide any assistance in being able to prevent the sound from playing and removing this event listener (through code on the main timeline if possible) it would be greatly appreciated.
Regards,
Darren
If stopping the sound is all that you want why not use SoundMixer.stopAll().
You can use it in the MOUSE_UP event after checking if the said movieclip's visible property.