How to stop the song from playing in Action Script 3 - actionscript-3

I am trying to stop the song from playing when user clicks on a button, this is my code :
var mySound:MainSound = new MainSound();
var cmyChannel :SoundChannel;
animation_play.addEventListener(MouseEvent.CLICK, playSound);
function playSound(event:Event) {
mySound.play();
}
animation_stop.addEventListener(MouseEvent.CLICK, stopSound);
function stopSound(event:Event) {
mySound.stop();
}
animation_play.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_3);
function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void
{
gotoAndPlay(2);
}
when i click on the animation_play object it works perfectly fine, it does as how it should be by playing the sound and starting the animation from the specified frame. however if i click on animation_stop object i get an error
TypeError: Error #1006: stop is not a function.
Anyone know how to go about fixing this ?

You need to set the mySound.play() to the cmyChannel object. Then call stop on cmyChannel. Here is the code:
var mySound:MainSound = new MainSound();
var cmyChannel :SoundChannel;
animation_play.addEventListener(MouseEvent.CLICK, playSound);
function playSound(event:Event) {
cmyChannel = mySound.play();
}
animation_stop.addEventListener(MouseEvent.CLICK, stopSound);
function stopSound(event:Event) {
cmyChannel.stop();
}
animation_play.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_3);
function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void
{
gotoAndPlay(2);
}

To stop your sound, you have to use a SoundChannel object like this, for example :
function playSound(event:Event): void
{
cmyChannel = mySound.play();
}
and
function stopSound(event:Event): void
{
cmyChannel.stop();
}
Hope that can help.

Related

as3 flash gotoAndPlay

I am making a small platformer game at flash and I just added the first enemy.
I get this in the "output":
" TypeError: Error #1006: gotoAndPlay is not a function.
at scratch_theGame_nojumping_tryEnemy_fla::MainTimeline/fl_enemyAlerted() "
My code on the enemy layer is the following:
var enemyAlerted:Boolean = false;
var alerted:Boolean = false;
var enemy:Object = new Object();
stage.addEventListener(Event.ENTER_FRAME, fl_alerted);
stage.addEventListener(Event.ENTER_FRAME, fl_enemyAlerted);
function fl_alerted(event:Event):void
{
if (char.x > 400)
{
alerted = true;
}
}
function fl_enemyAlerted(event:Event):void
{
if (alerted = false)
{
enemy.gotoAndPlay("alert");
}
if (alerted = true)
{
enemy.gotoAndPlay("chase");
}
}
My goal here is to make the enemy loop the 'alert' animation from the start until my character has come close enough to be noticed, and then the enemy should play the 'chase' animation. Can anyone help? Thank you in advance.

addChild and removeCurrent child

I have multiple buttons that load multiple movie clips. My problem is that when one loads it does not go away prevent the others to load when their button is clicked. Here is my code below. Do I need to add and "if" statement? Any help would be greatly appreciated. Thanks.
movieButton.addEventListener(MouseEvent.CLICK, gotoMovie);
webButton.addEventListener(MouseEvent.CLICK, gotoWeb);
mailButton.addEventListener(MouseEvent.CLICK, gotoMail);
function gotoMovie(event:Event):void {
var moviescene:MovieClip = new movie();
stage.addChild(moviescene);
}
function gotoWeb(event:Event):void {
var webscene:MovieClip = new web();
stage.addChild(webscene);
}
function gotoMail(event:Event):void {
var contactscene:MovieClip = new contact();
stage.addChild(contactscene);
}
I'm not entirely sure what you want to achieve, but I guess this should help.
var currentScene:MovieClip; // pointer to scenes
movieButton.addEventListener(MouseEvent.CLICK, gotoMovie);
webButton.addEventListener(MouseEvent.CLICK, gotoWeb);
mailButton.addEventListener(MouseEvent.CLICK, gotoMail);
// function that sets new scenes
function setScene(mc:MovieClip):void {
if(currentScene) stage.removeChild(currentScene);// if a scene is already loaded, remove it
currentScene = mc; // set currentScene to new scene
stage.addChild(currentScene); // add it to stage
}
function gotoMovie(event:MouseEvent):void {
setScene(new movie());
}
function gotoWeb(event:MouseEvent):void {
setScene(new web());
}
function gotoMail(event:MouseEvent):void {
setScene(new contact());
}

How to remove a tween before moving to next frame in as3

I have a movieclip which spins. I want it when users drag and drop it to stop spinning and be in it's initial position. I wrote this code but i get error TypeError: Error #1009: Cannot access a property or method of a null object reference.
at omoixes10_fla::MainTimeline/EntFrame() when I move to next frame. I can not see what i did wrong. Can you please help me with my code? Do I have to remove tween before I move to next frame?
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
tick1.parent.removeChild(tick1);
wrong1.parent.removeChild(wrong1);
sentences2.buttonMode=true;
sentences1.buttonMode=true;
Piece1_mc.buttonMode=true;
var my_x:int=stage.stageWidth
var my_y:int=stage.stageHeight
var myWidth:int=0-my_x;
var myHeight:int=0-my_y;
var boundArea:Rectangle=new Rectangle(my_x, my_y, myWidth ,myHeight);
var spin:Tween=new Tween(Piece1_mc, "rotation",Elastic.easeInOut,0,360,5,true);
spin.stop();
sentences2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
snoopy.gotoAndPlay(2);
addChild(tick1);
addChild(wrong1);
sentences2.removeEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
sentences1.removeEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
spin.start();
spin.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
e.target.yoyo();
{
Piece1_mc.addEventListener(MouseEvent.MOUSE_DOWN, DragP1);
function DragP1 (event:MouseEvent):void
{
Piece1_mc.startDrag();
Piece1_mc.startDrag(false,boundArea);
spin.stop();
}
stage.addEventListener(MouseEvent.MOUSE_UP, DropP1);
function DropP1(event:MouseEvent):void
{
Piece1_mc.stopDrag();
}
if(Targ1_mc.hitTestObject(Piece1_mc.Tar1_mc)) {
Piece1_mc.x=677;
Piece1_mc.y=48,10;
myTimer.start();
spin.stop();
}
}
}
sentences1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
snoopy.gotoAndPlay(64);
}
var myTimer:Timer = new Timer(2000,1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void {
gotoAndStop(15);
if (Piece1_mc.parent)
{
Piece1_mc.parent.removeChild(Piece1_mc);
}if (tick1.parent)
{
tick1.parent.removeChild(tick1);
}
if (wrong1.parent)
{
wrong1.parent.removeChild(wrong1);
}
}
}
So the first problem was adding your event listeners inside an enter frame which was not the right way to do since it will keep on adding event listeners at every frame.
Second, you should use a mouse move event listener as I've recommended before to track and test your hit test.
Thirdly, since you are rotating your MovieClip, and you want it to go back to its initial state, you should do :
Piece1_mc.rotation=0;
Hope this helps.

AS3 Flash Animation not fully working in captivate 5.5

Hi I have done a few Flash Animations previously in AS3 all of which import and run fine in Captivate 5.5. However, One of them, a simple drag an drop game won't work. It imports and is visible in captivate everything works with one (irritating) problem. That is, the objects will not drop onto the appropriate drop zones. The animation works fine as an SWF in my browser but just will not function when put into captivate
any ideas? The outline of the code is below. I am tearing my hair out, any advice would be greatly appreciated.
code:
right_mc.visible=false;
wrong_mc.visible=false;
var orig1X:Number=item1_mc.x;
var orig1Y:Number=item1_mc.y;
item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);
item1_mc.buttonMode=true;
function dragTheObject(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.startDrag();
var topPos:uint=this.numChildren-1;
this.setChildIndex(item, topPos);
}
function item1Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone1_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone1_mc.x;
item.y=dropZone1_mc.y;
} else {
item.x=orig1X;
item.y=orig1Y;
}
};
done_btn.addEventListener(MouseEvent.CLICK,checkAnswers);
function checkAnswers(event:MouseEvent):void {
if (dropZone1_mc.hitTestPoint(item1_mc.x,item1_mc.y) &&
dropZone16_mc.hitTestPoint(item16_mc.x,item16_mc.y)) {
wrong_mc.visible = false;
right_mc.visible = true;
} else {
wrong_mc.visible = true;
right_mc.visible = false;
}
}
reset_btn.addEventListener(MouseEvent.CLICK,reset);
function reset(event:MouseEvent):void {
item1_mc.x=orig1X;
item1_mc.y=orig1Y;
right_mc.visible=false;
wrong_mc.visible=false;
}
Because hitTestPoint only works with global coordinates.
When you open the SWF in the browser, local and global coordinates are the same, that's why it works. But when you load it inside Captivate, they differ.
Try this:
import flash.geom.Point;
// ...
var localPoint:Point = new Point(item.x, item.y);
var globalPoint:Point = item.parent.localToGlobal(localPoint);
if (dropZone1_mc.hitTestPoint(globalPoint.x, globalPoint.y)) {
item.x = dropZone1_mc.x;
item.y = dropZone1_mc.y;
}
// ...

Making loaded image draggable actionscript 3

i want to load an image to stage using Loader and then I want to make it draggable. Loading is done by selecting from tilelist as seen below, but I have no idea about drag and drop in AS3, can anybody help me? I want to make it as simple as possible.
Here is my code to load image:
var charge1:Loader = new Loader();
addChild(charge1);
this.addChildAt(charge1, getChildIndex(bg));
charge1.x = 280;
charge1.y = 270;
...
function setCharge1(e:Event):void{
trace(e.target.selectedItem.source);
this.charge1.load(new URLRequest(e.target.selectedItem.source));
this.charge1.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
}
yuku was going along the right lines but what you want to do is get the contents of the loaderInfo which is actually the clip that is loaded in. something like
private function onComplete(event : Event) : void
{
var loadedClip : MovieClip = LoaderInfo(event.target).content;
loadedClip.addEventListener(MouseEvent.MOUSE_DOWN, function(event : MouseEvent)
{
loadedClip.startDrag();
});
stage.addEventListener(MouseEvent.MOUSE_UP, function(event : MouseEvent)
{
loadedClip.stopDrag();
});
}
This is the easiest way to me...
/* Load Image */
var myLoader:Loader = new Loader();
imageContainer.addChild(myLoader);
var url:URLRequest = new URLRequest("photo.jpg");
myLoader.load(url);
/* Drag and Drop */
imageContainer.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
function pickUp(event:MouseEvent):void
{
imageContainer.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, dopIt);
function dopIt(event:MouseEvent):void
{
imageContainer.stopDrag();
}
Use the following code:
this.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);
this.addEventListener(MouseEvent.MOUSE_UP, dropMovie);
this.buttonMode = true;
private function dragMovie(event:MouseEvent):void
{
this.startDrag();
}
private function dropMovie(event:MouseEvent):void
{
this.stopDrag();
}
Loader is a subclass of Sprite, so you can use startDrag and stopDrag method.
For example:
charge1.addEventListener("mouseDown", function() {
charge1.startDrag();
});
stage.addEventListener("mouseUp", function() {
charge1.stopDrag();
});