Actionscript acting funny - actionscript-3

I was watching a youtube tutorial about as3,I tried to copy the code to see how it works, but even though it worked for the guy in the video, it didn't work for me..
Video Link(update): https://www.youtube.com/watch?v=pKj1IgWJd-A&ab_channel=WaarithAbdul-Majeed
(short post update)I made this late at night and didn't really include a lot of stuff in the post about the problem, so pretty much the movie clip is supposed to follow the mouse but it doesn't even though I made the code for it.
The errors are about circle.x = content.mouseX;
circle.y = content.mouseY;
Flash keeps popping up "1120: Access of undefined property content."
Code:
addEventListener(Event.ENTER_FRAME,shipgo);
function shipgo(e:Event){
ship_b.x=mouseX;
ship_b.y=mouseY;
}

Try referencing the stage for its mouse coordinates:
stage.addEventListener( Event.ENTER_FRAME, shipgo );
function shipgo ( e:Event ) :void
{
ship_b.x = stage.mouseX;
ship_b.y = stage.mouseY;
}

Related

How to play animation and then play reverse on hover, start playing again until end on hover out using in Adobe Animate (Flash)?

Sorry this is so specific but I have combed through so many pages and videos and tutorials and can't figure this out.
I have all of my animations within a MovieClip. In the movie clip is also a stage sized white square button with the instance name "btn". Back on the main stage I have a second layer called "actions" with the following code applied to the first (and only) frame. It's not working. At all. (HUGE) tia
stop(); // this will stop the movie from playing at the start
btn.addEventListener((MouseEvent.ROLL_OVER, playMovie);
btn.addEventListener((MouseEvent.ROLL_OUT, stopMovie);
function playMovie(evt:MouseEvent):void {
play();
}
function stopMovie(evt:MouseEvent):void {
stop();
}
The problem is when you say play(); or stop(); which object are you really commanding? Your playMovie function could be in theory used to control many MovieClips at once, in different ways, so be specific with your commands...
btn.play(); //start the playback of "btn" MC
btn.stop(); //stop the playback of "btn" MC
Also consider using MOUSE_OVER/OUT instead ROLL_OVER/OUT etc but whatever works for you.
For reversing you will use btn.prevFrame(); together with an ENTER_FRAME event function. This function reads your Document settings for the FPS. For example, if you set 30 frames-per-sec then whatever instructions you put inside the event function will be processed 30 times per second.
See this other Answer for advice about reversing the playback of a MovieClip.
#VC.One is correct in how you should implement a solve to your issue, however in response to your comment on their answer, I thought I would demonstrate how to implement this fully for you - incase they don't.
var removeUpdate = false;
btn.addEventListener(MouseEvent.MOUSE_OVER, playMovie);
btn.addEventListener(MouseEvent.MOUSE_OUT, stopMovie);
function playMovie(evt:MouseEvent):void {
// Stop rewinding the movie clip and play it
if(removeUpdate){
stage.removeEventListener(Event.ENTER_FRAME, update);
removeUpdate = false;
}
// play our button
btn.play();
}
function stopMovie(evt:MouseEvent):void {
// stop our button
btn.stop();
// ... and rewind it
stage.addEventListener(Event.ENTER_FRAME, update);
removeUpdate = true;
}
function update(evt: Event){
// moves the button movie clip backwards one frame.
btn.prevFrame();
// If we have finished rewinding the movie clip, then stop
if(btn.currentFrame == 1){
stage.removeEventListener(Event.ENTER_FRAME, update);
removeUpdate = false;
}
}
It is important that you remove the update event because if you don't, the movie will never play again, because it will go one frame forward and then back again every frame due to; btn.play(); btn.prevFrame();

stopping a soundchannel through a movieclip

my question is as follows. I have a sound triggered by clicking on a movieclip, like this:
var audioPlayer: SoundChannel = new SoundChannel();
var SndJuichen : Sound = new Juichen();
bootje.addEventListener(MouseEvent.CLICK, bootjeClick);
function bootjeClick (e:MouseEvent):void{
audioPlayer = SndJuichen.play();
}
}
and i want to stop this by clicking on another movieclip. for the time being, i have not been able to do this.
so I tried it by clicking on a button. like this:
stop_btn.addEventListener(MouseEvent.CLICK, audioStop);
function audioStop (e:MouseEvent):void{
addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
function mousedown (e:MouseEvent){
audioPlayer.stop();
}
}
At first this worked but, but when i tried it later without changing the code it didn't work anymore.
so my question is how do i get the soundchannel to stop?
I have searched for hours already, if you think you have a link that can easily fix my problem its okay too.
sincerely
Drop the very first line var audioPlayer: SoundChannel = new SoundChannel(); and make sure this code is run in the context of some persistent MovieClip, this way audioPlayer will turn into a property of that MovieClip and be accessible from another frame where you decide to stop the sound.

How to control sound/music playblack in AS3

I'm making a game with AS3. I would like to put a different music in each background.
So far, I've succeed to start the music when the player enters in a scene. But if he goes out, the music is still playing...(I want the music to stop when the players leave a scene).
I don't understand why the music is still playing.. Here's my code :
public function newBackground(thisBack:String):void{
var room = back.currentBack;
switch (thisBack){
case "maisonExt":
var mySound:Sound = new exterieur ();
mySound.play ();
My mp3 file is named "exterieur.mp3".
If you can think of anything it would be a great help.
Thanks !
You need to stop the music again. If you don't stop it, it will keep on playing :)
When you call .play it will return a SoundChannel which lets you control the playback -- changing position, stopping the playback, and accessing the SoundTransform which lets you control the volume. Here is some example code for you:
public var currentSoundChannel:SoundChannel;
public function newBackground(thisBack:String):void
{
var room = back.currentBack;
if (currentSoundChannel != null)
{
currentSoundChannel.stop()
}
var nextSong:Sound;
switch (thisBack){
case "maisonExt":
nextSong = new exterieur ();
break;
}
currentSoundChannel = nextSong.play();
}
If you want to learn more about sound, I would recommend this tutorial http://gamedev.michaeljameswilliams.com/2009/03/03/avoider-game-tutorial-9/. It's part of a larger tutorial which teaches you many aspects of game making, taking you from novice to capable of creating a game. You should really check that out.

Action Script 2 Suggestion

I am doing a project which is about interacting, mouse on screen, Hence, I want to do it by using Adobe Flash Pro and Action Script2.
here is the Task:
- a simple animations (for example: that full of spiders walking/jumping from up to down on the screen)
- once the mouse moves, the animation will Reverse (all spiders animation will reverse back and hide back to the top of screen from the place that the came).
- if the mouse doesn't move after 60 Seconds the animation will happen again till the mouse move again on the screen (Those spiders animation will happen again).
i have created a Animation Spider "Movie clip" (spider animation going down)
1- what script i should write to make animation reverse?
2- how can i tell Adobe my mouse is moved or it's not moving, so the spiders animation will happen or reverse?
by the way, I am a very beginner in action script 2.0
i appreciate any suggestion and help *is fine for me to do it in action script 3 too
Thanks.
Oh my. AS2 :)
OK first off I think it would be easier if you would create 2 different animations. One for the Spider to walk down. The other one to walk up. It is possible to reverse a MovieClip but I think if you`re a beginner, stick with the basics.
You need 3 thinks here.
1) Spider Clips. Going down and up.
2) An interval (timer in AS3)
var interval:Number = setInterval(spidersComeOut, 60 * 1000);
3) Mouse move listener
root.onMouseMove = function()
{
//swap your spider clips
//move the spider up again
//reset the interval with clearInterval(interval) and restart it again.
}
This is a very basic handler for mouse move.
Hope this will help you a little bit. This is not a complete solution. It will not work out of the box.
One thing at the end. If you are new to AS2 I would recomend to give as3 a shot. It is more difficult to start with, but there are more people out there willing to help with an AS3 problem then with as2.
Since you said you were ready to use AS3 here is solution.
I assume you have a separate movie clip containing your spiders animation which you place on the main timeline/stage.
1.place your MovieClip on the stage and give it the instance name of 'spiders'.
2.inside this MovieClip, on the first frame put this code (it will handle revesing the animation)
import flash.events.Event;
stop();
var _dir:int = 1;
addEventListener(Event.ENTER_FRAME, onEF);
function onEF(e:Event):void
{
getNextAnimationFrame();
}
function getNextAnimationFrame():void
{
var frameNum:int = currentFrame + _dir;
if (frameNum < 1 || frameNum > totalFrames)
{
removeEventListener(Event.ENTER_FRAME, onEF);
}
frameNum = Math.max(1, Math.min(totalFrames,frameNum));
gotoAndStop(frameNum);
}
function changeDirection($dir:int):void
{
_dir = $dir;
removeEventListener(Event.ENTER_FRAME, onEF);
addEventListener(Event.ENTER_FRAME, onEF);
}
3.on the main timeline (in the first frame) put this code:
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
var timer:Timer = new Timer(60 * 1000, 1);
timer.start();
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTime);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
function onTime(e:TimerEvent):void
{
trace("it's time");
spiders.changeDirection(1);
}
function onMove(e:MouseEvent):void
{
timer.reset();
timer.start();
spiders.changeDirection(-1);
}
It would be possible to handle mouse, timer and animation in one piece of code, however the way it's build now is kind of OOP, if at some point you decided to build it properly it would be extremely easy to 'rewrite' this code as separate classes.

How to make a symbol remove itself at a certain frame?

I'm struggling on doing a simple thing, I have an object that generates a smoke trail by creating and adding a simple animation done in flash. All works fine but I can't seem to find an easy way to make the smoke remove itself after playing the animation, if i add something like this.parent.removeChild(this); to the last frame of the animation the app simply crashes with Cannot access a property or method of a null object reference.
what am i doing wrong?
the animation keeps playing even thought the object is no longer being rendered, at the second call of this.parent.removeChild(this); the object was no longer parented to any object, stopping the animation solved the problem
thank you frankhermes
this.parent.removeChild(this);
this.stop();
in your main Movie clip assigned below code to a button(show button):
var swfLoader:Loader = new Loader();
stage.addChild(swfLoader);
var swfURL:URLRequest = new URLRequest("yourchildSWF.swf");
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
swfLoader.load(swfURL);
function loadComplete(e:Event):void
{
//swfLoader.x = (stage.stageWidth-swfLoader.width)/2; //for center align
//swfLoader.y = (stage.stageHeight-swfLoader.height)/2; //for center align
swfLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
swfLoader.content.addEventListener(MouseEvent.CLICK, function(e:Event)
{
swfLoader.unload();
} );
}
you must have a button in your child swf. when pressed on it, swf will be closed.(create a symbole as button for close button)
does not need any code in child swf.