I am new to flash and using as3. I am in the process of making a simple catching game where the items fall from the top and you control a basket at the bottom to catch them. My script is fine and is playing without erros throughout which I am happy about, but how would I add a sound clip to this script to play when the item lands in the basket? Thanks in advance!!!
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
var catcher:Catcher;
var createEnemyID:uint;
var gameSpeed:uint;
var droppedText:TextField;
var caughtText:TextField;
var score:uint=0;
function initGame():void{
catcher=new Catcher();
catcher.x=500;
catcher.y=1400;
addChild(catcher);
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
Mouse.hide();
gameSpeed=500;
createEnemyID=setInterval(createEnemy,gameSpeed);
droppedText=new TextField();
droppedText.x=50;
droppedText.y=50;
addChild(droppedText);
caughtText=new TextField();
caughtText.x=250;
caughtText.y=50;
addChild(caughtText);
droppedText.text=caughtText.text='0';
}
function moveCatcher (e:MouseEvent):void{
catcher.x=this.mouseX;
e.updateAfterEvent();
}
function createEnemy():void{
var enemy:Faller=new Faller();
enemy.y=-1;
enemy.x=Math.random()*stage.stageWidth;
enemy.addEventListener (Event.ENTER_FRAME, dropEnemy);
addChild(enemy);
}
function dropEnemy(e:Event):void{
var mc:Faller=Faller(e.target);
mc.y+=15;
if(mc.hitTestObject(catcher)) {
caught(mc);
}
else if (mc.y>stage.stageHeight){
dropped(mc);
}
}
function caught(mc:Faller):void{
mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
caughtText.text=String(Number(caughtText.text)+1);
}
function dropped(mc:Faller):void{
mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
droppedText.text=String(Number(droppedText.text)+1);
if(droppedText.text=='5'){
gameOver();
}
}
function gameOver():void{
score=Number(caughtText.text);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
removeChild(catcher);
clearInterval(createEnemyID);
removeChild(caughtText);
removeChild(droppedText);
while(numChildren>0){
getChildAt(0).removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChildAt(0);
}
Mouse.show();
gotoAndStop('gameover');
}
initGame();
import the sound into flash.
edit the properties and set the class of the sound to MySoundClass or whatever you like but you have to reference it later.
In your code write the following in the collision method.
var sound:Sound = new MySoundClass();
sound.play();
See this AS3 Sound tutorial
Related
Using a code snippet in as3 flash, but struggling for it to actually work:
I would like to unload the child SWF (which is a video) and go back into the parent SWF (a video selection page). Tried so many different ways and need a quick and easy solution. Thanks.
Below does not seem to work...
exit_btn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);
import fl.display.ProLoader;
var fl_ProLoader:ProLoader;
//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad:Boolean = true;
function fl_ClickToLoadUnloadSWF(event:MouseEvent):void
{
fl_ProLoader.unload();
removeChild(fl_ProLoader);
fl_ProLoader = null;
}
I use this code it's tested
import flash.filesystem.*;
import flash.events.MouseEvent;
import flash.net.*;
import flash.events.*;
var _file: File;
_file = File.documentsDirectory.resolvePath("./Directory to swf file/mySwf.swf");
if (_file.exists) {
trace("SWF loading ...........");
displayingSWF(_file.nativePath)
} else {
trace("the file doesn't exit");
}
//////////////// DIsplay the SWF story file //////////////////
var mLoader: Loader ;
function displayingSWF(lnk) {
var inFileStream: FileStream = new FileStream();
inFileStream.open(_file, FileMode.READ);
var swfBytes: ByteArray = new ByteArray();
inFileStream.readBytes(swfBytes);
inFileStream.close();
mLoader = new Loader();
var loaderContext: LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true;
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoadComplete);
mLoader.loadBytes(swfBytes, loaderContext);
}
function onSwfLoadComplete(e: Event): void {
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onSwfLoadComplete);
addChild(mLoader);
}
exit_btn.addEventListener(MouseEvent.CLICK, dimising);
function dimising(e: MouseEvent): void {
mLoader.unloadAndStop();
removeChild(mLoader);
}
Below is the AS3 code taken from https://www.youtube.com/watch?v=GQnoOcPbgx0. Currently there are 5 buttons. I would like the –
btnPlay to control both the player.playVideo(); and the player.pauseVideo();
btnMute to control both the player.mute(); and player.unMute();
buttons to appear on rollover only
You can view a demo here (rollover left orange button to view video) http://pages.areadevelopment.com/advertisers/MDA/index.html
If anyone can help, it would be greatly appreciated.
Thank you! CE
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.ui.MouseCursor;
Security.allowDomain("www.youtube.com");
var player:Object;
var loader:Loader = new Loader();
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
loader.contentLoaderInfo.addEventListener(Event.INIT, onInit);
loader.x = 344;
loader.y = 34;
function onInit(e:Event):void{
addChild(loader);
player = loader.content;
player.addEventListener("onReady",onPlayerReady);
}
function onPlayerReady(e:Event):void{
player.setSize(330,225);
player.loadVideoById("nHnXVBNizDQ",0);
}
btnPlay.addEventListener(MouseEvent.CLICK, playVid);
function playVid(e:MouseEvent):void{
player.playVideo();
}
btnPause.addEventListener(MouseEvent.CLICK, pauseVid);
function pauseVid(e:MouseEvent):void{
player.pauseVideo();
}
btnStop.addEventListener(MouseEvent.CLICK, stopVid);
function stopVid(e:MouseEvent):void{
player.stopVideo();
}
btnMute.addEventListener(MouseEvent.CLICK, muteVid);
function muteVid(e:MouseEvent):void{
player.mute();
}
btnUnmute.addEventListener(MouseEvent.CLICK, UnmuteVid);
function UnmuteVid(e:MouseEvent):void{
player.unMute();
}
I'm making a little interactive ad for an album. It has 4 draggable song titles, that when dropped on a target, starts the song. Trying to figure out how to stop the song that's playing when a new song is started. Here's my code:
package {
import flash.display.*;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class Deftones extends MovieClip{
function Deftones() {
swerve1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
swerve1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
polt1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
polt1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
rd1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
rd1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
gauze1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
gauze1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
var sound1:Sound= new Sound();
var sound2:Sound= new Sound();
var sound3:Sound= new Sound();
var sound4:Sound= new Sound();
sound1.load(new URLRequest("music/Swerve_City.mp3"));
sound2.load(new URLRequest("music/Poltergeist.mp3"));
sound3.load(new URLRequest("music/Romantic_Dreams.mp3"));
sound4.load(new URLRequest("music/Gauze.mp3"));
var channel:SoundChannel= new SoundChannel();
function dragStartS(e:MouseEvent){
e.currentTarget.startDrag();
}
function dragEndS(e:MouseEvent){
e.currentTarget.stopDrag();
if (swerve1.hitTestObject(speaker1)){
channel.stop();
sound1.play(0);
}
else if (polt1.hitTestObject(speaker1)){
channel.stop();
sound2.play(0);
}
else if (rd1.hitTestObject(speaker1)){
channel.stop();
sound3.play(0);
}
else if (gauze1.hitTestObject(speaker1)){
channel.stop();
sound4.play(0);
}
}
}
}
Any help would me much appreciated.
You can use SoundMixer.stopAll()
Stops all sounds currently playing.
You can use SoundChannel class for this, look in this link (Section 4 - Stopping a Sound)
Trying to figure out a code for my ship when i press spacebar and fires the laser a sound will play
private var reqButton:URLRequest = new URLRequest("laser.mp3");
private var buttonSound:Sound = new Sound(reqButton);
if (key.isDown(Keyboard.SPACE))
{
fireBullet();
buttonSound.play();
}
ok this worked the only 2 things i need was to add
import flash.net.URLRequest;
import flash.media.Sound;
You can try this if you have an external sound in an "audio" folder
var reqButton:URLRequest = new URLRequest("audio/button.mp3");
var buttonSound:Sound = new Sound(reqButton);
and then your keypress code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
function keyDown(event:KeyboardEvent):void
{
switch(event.charCode)
{
case 32: //Space Bar
//trace("Space Bar");
buttonSound.play();
break;
}
}
I'm developing an AS 3.0 wrapper to add some extra stuff that has to load some old and plain frame to frame SwishMax 3 animations and then be able to stop them, play them, and so...
Here is my code:
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.events.*;
[SWF(backgroundColor="#ffffff", frameRate="17", width="300", height="250")]
public class SwishMaxWrapper extends Sprite {
function SwishMaxWrapper() {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
addChild(loader);
var request:URLRequest = new URLRequest("swishy.swf");
loader.load(request);
}
private function completeHandler(event:Event):void {
var movie:MovieClip = event.target.content;
movie.stop();
}
}
}
The animation load works as expected but the movie.stop() doesn't. What is wrong?
I tested your code and it works on my machine. The SWF that I loaded had its animation in the main timeline. Is it possible that the swishy.swf has animation that is not on the main timeline? Perhaps the animation is in another symbol instead, and an instance of that symbol is on the stage. Anyway, when you call stop() in your code above, it's just telling the main timeline to stop, but other movie clips on the stage will keep on going.
I think that's what Simsoft was pointing out.
I tested your code using a SWF that had a symbol on the stage that had animation in it, and I got the problem that you are describing. I fixed it by modifying completeHandler() as follows:
public function completeHandler(event:Event):void {
var movie:MovieClip = event.target.content;
movie.stop(); //doesn't work - main timeline is only one frame long
for(var i:int = 0; i<movie.numChildren; i++) {
var child:MovieClip = movie.getChildAt(i) as MovieClip;
if(child) { //need this test - if the cast to MovieClip fails, child will be null
child.stop(); //works
}
}
}
Hopefully you don't have more animations nested at deeper layers. If that's the case, you'll have to modify this to keep peering into the children of each child and trying to stop their timelines as well.
Anyway, hope that helps. Good luck!
stop() isn't recurive, I think the problem is here.
function ruleThemAll(target : DisplayObjectContainer, doStop : Boolean = true) : void
{
for(var i : uint = 0; i < target.childNum; ++i)
{
var child : DisplayObject = target.getChildAt(i);
// If it's a container, go into to stop children
if(child is DisplayObjectContainer)
ruleThemAll(child as DisplayObjectContainer, doStop);
if(child is MovieClip)
{
if(doStop)
MovieClip(child).stop();
else
MovieClip(child).play();
}
}
}