FlashCS6 - Actionscript 3 - Loading SWFs - All playing but one - actionscript-3

I'm just playing around trying to get a navigation bar to work and I'm having a spot of trouble. I've got a main.swf, and inside it is code to load in a circle.swf, square.swf and triangle.swf on their respective button clicks. The circle and square load in and play correctly, however the triangle loads, but doesn't play for some reason. The code, to my eye, is the same for all three, but only that one doesn't play.
Note sure where exactly the bug might be, but below it the code referring to the specific swfs. Let me know if it's helpful to see more though.
var _swfPathArr:Array = new Array("Subfolder/Circle.swf", "Subfolder/Subsubfolder/Square.swf", "Triangle.swf");
and
button_Circle.addEventListener(MouseEvent.CLICK, setContent);
button_Square.addEventListener(MouseEvent.CLICK, setContent);
button_Triangle.addEventListener(MouseEvent.CLICK, setContent);
And the method actually displaying and clearing the loaded swfs:
function setContent(event:MouseEvent):void
{
var _swfToAdd:MovieClip;
switch(event.target.name)
{
case "button_Circle":
_swfToAdd = _swfClipsArr[0];
gotoAndStop(2);
break;
case "button_Square":
_swfToAdd = _swfClipsArr[1];
gotoAndStop(3);
break;
case "button_Triangle":
_swfToAdd = _swfClipsArr[2];
gotoAndStop(4);
break;
}
Window_content.removeChildAt(Window_content.numChildren - 1);
Window_content.addChild(_swfToAdd);
}
Any help you can offer would be greatly appreciated.
EDIT:
All the code
import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.IEventDispatcher;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.events.MouseEvent;
var _swfLoader:Loader;
var _swfRequest:URLRequest;
var _swfPathArr:Array = new Array("Subfolder/Circle.swf", "Subfolder/Subsubfolder/Square.swf", "Triangle.swf");
var _loadedSWFs:int;
var _swfClipsArr:Array = new Array();
var _swfTempClip:MovieClip;
startLoading(_swfPathArr);
function startLoading(pathArr:Array):void
{
_swfLoader = new Loader();
_swfRequest = new URLRequest();
addChildAt(_swfLoader, 0);
_loadedSWFs = 0;
loadSWF(pathArr[0]);
}
function loadSWF(path:String):void
{
setUpListeners(_swfLoader.contentLoaderInfo);
_swfRequest.url = path;
_swfLoader.load(_swfRequest);
}
function setUpListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE, onSwfComplete);
dispatcher.addEventListener(ProgressEvent.PROGRESS, currentSwfProgress);
}
function currentSwfProgress(event:ProgressEvent):void
{
var _perc:int = (event.bytesLoaded / event.bytesTotal) * 100;
//swfPreloader.precentTF.text = _perc + "%";
}
function onSwfComplete(event:Event):void
{
event.target.removeEventListener(Event.COMPLETE, onSwfComplete);
event.target.removeEventListener(ProgressEvent.PROGRESS, currentSwfProgress);
_swfTempClip = event.target.content;
_swfTempClip.customID = _loadedSWFs;
_swfClipsArr.push(_swfTempClip);
if(_loadedSWFs < (_swfPathArr.length - 1))
{
_loadedSWFs++;
loadSWF(_swfPathArr[_loadedSWFs]);
}
else
{
_swfLoader.unloadAndStop();
_swfLoader = null;
onCompletePreloading();
}
}
function onCompletePreloading():void
{
button_Circle.addEventListener(MouseEvent.CLICK, setContent);
button_Square.addEventListener(MouseEvent.CLICK, setContent);
button_Triangle.addEventListener(MouseEvent.CLICK, setContent);
}
function setContent(event:MouseEvent):void
{
var _swfToAdd:MovieClip;
switch(event.target.name)
{
case "button_Circle":
_swfToAdd = _swfClipsArr[0];
gotoAndStop(2);
break;
case "button_Square":
_swfToAdd = _swfClipsArr[1];
gotoAndStop(3);
break;
case "button_Triangle":
_swfToAdd = _swfClipsArr[2];
gotoAndStop(4);
break;
}
Window_content.removeChildAt(Window_content.numChildren - 1);
Window_content.addChild(_swfToAdd);
//trace(_swfToAdd.customID);
}
stop();
So thanks to Cherniv suggesting to switch the order of the SWFs and see what happens, it seems the problem is related to the third element in the path array. Just need a pair of outside eyes to look over the code and they'll probably be able to spot some very obvious reason for my trouble, haha. If anybody can spot it, that would be greatly appreciated.

Related

Remove all created symbols from stage

I don't usually do this, but i'm lost here.
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
var first_tile:colors;
var second_tile:colors;
var pause_timer:Timer;
var game_timer:Timer;
var colordeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6);
function color_match() {
game_timer = new Timer(10000,1);
for (x=1; x<=4; x++) {
for (y=1; y<=3; y++) {
var random_card = Math.floor(Math.random()*colordeck.length);
var tile:colors = new colors();
tile.col = colordeck[random_card];
colordeck.splice(random_card,1);
tile.gotoAndStop(7);
tile.x = ((x-1)*70)+30;
tile.y = ((y-1)*100)+30;
tile.addEventListener(MouseEvent.CLICK,tile_clicked);
game_timer.addEventListener(TimerEvent.TIMER_COMPLETE,end_game);
addChild(tile);
}
}
game_timer.start();
}
function tile_clicked(event:MouseEvent) {
var clicked:colors = (event.currentTarget as colors);
if (first_tile == null) {
first_tile = clicked;
first_tile.gotoAndStop(clicked.col);
}
else if (second_tile == null && first_tile != clicked) {
second_tile = clicked;
second_tile.gotoAndStop(clicked.col);
if (first_tile.col == second_tile.col) {
pause_timer = new Timer(1000,1);
pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
pause_timer.start();
}
else {
pause_timer = new Timer(1000,1);
pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
pause_timer.start();
}
}
}
function reset_tiles(event:TimerEvent) {
first_tile.gotoAndStop(7);
second_tile.gotoAndStop(7);
first_tile = null;
second_tile = null;
pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
}
function remove_tiles(event:TimerEvent) {
removeChild(first_tile);
removeChild(second_tile);
first_tile = null;
second_tile = null;
pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
}
function end_game(event:TimerEvent) {
}
This is a little colour matching game. Click two tiles, they dissappear if matched, or turn back to grey if not. The loop creates instances of colour, in randomly placed pairs, and sets them to frame 7 (grey colour).
I cant work out how to remove any remaining colour blocks when the game time hits zero. Everything i try is throwing errors. The idea is then to let people play again, or a win script.
You don't have to necessarily code it for me, i just need to understand the process! Thanks.
I believe the best way is to create a container, so you can add all tiles and manage them on the best way you decide to.
var tileContainer:Sprite = new Sprite();
addChild(tileContainer);
// instead of addChild(tile);
tileContainer.addChild(tile);
// to remove all tiles
tileContainer.removeChildren();

Flash CS5.5: Error 1046. I can't find another way to do it

Essentially, I have 6 balls, and when my showBalls() function runs, I want each ball (each are a movieclip) to run its animation and tween to the correct place. I want them to do it in order though. So 1 ball tweens, then the next, then the next and so on.
I've used a bunch of if statements because switch wouldn't work, but now it just keeps throwing me Error 1046 and telling me that "Event" was not found or not compile-time constant.
I can't see anyway around this and it's frustrating me.
Here's some code for you all. It's probably messy as hell and there'll be a much easier way to do this. But I'm pretty new to AS3 so I can't see any other way.
I have tried to find the answer on here and somebody told me to un-nest the functions. So I did. I've not had a problem with one nest but I tried two here and it didn't work. So I un-nested, but to no avail. If there is a way around this, i'd be grateful for guidance.
So yeah, first ball shown, tweens, once tween reaches final frame, it stops and the number associated with it shows. Repeat for following 6 balls.
function showNumbers()
{
var count:int = 0;
var showTimer:Timer = null;
showTimer = new Timer(3125,8);
showTimer.start();
showTimer.addEventListener(TimerEvent.TIMER, showBalls);
function showBalls(Event:TimerEvent)
{
ball1.addEventListener(Event.ENTER_FRAME, ball1stop);
ball2.addEventListener(Event.ENTER_FRAME, ball2stop);
ball3.addEventListener(Event.ENTER_FRAME, ball3stop);
ball4.addEventListener(Event.ENTER_FRAME, ball4stop);
ball5.addEventListener(Event.ENTER_FRAME, ball5stop);
ball5.addEventListener(Event.ENTER_FRAME, ball6stop);
bonusBall.addEventListener(Event.ENTER_FRAME, bonusBallstop);
function ball1stop(event:Event):void
{
if (currentFrame == stopFrame1)
{
ball1.stop();
programNumber1.text = drawnArray[0];
ball1.removeEventListener(Event.ENTER_FRAME, ball1stop);
}
}
function ball2stop(event:Event)
{
if (currentFrame == stopFrame2)
{
ball2.stop();
programNumber2.text = drawnArray[1];
ball2.removeEventListener(Event.ENTER_FRAME, ball2stop);
}
}
function ball3stop(event:Event)
{
if (currentFrame == stopFrame3)
{
ball3.stop();
programNumber3.text = drawnArray[2];
ball3.removeEventListener(Event.ENTER_FRAME, ball3stop);
}
}
function ball4stop(event:Event)
{
if (currentFrame == stopFrame4)
{
ball4.stop();
programNumber4.text = drawnArray[3];
ball4.removeEventListener(Event.ENTER_FRAME, ball4stop);
}
}
function ball5stop(event:Event)
{
if (currentFrame == stopFrame5)
{
ball5.stop();
programNumber5.text = drawnArray[4];
ball5.removeEventListener(Event.ENTER_FRAME, ball5stop);
}
}
function bonusBallstop(event:Event)
{
if (currentFrame == stopFrame7)
{
bonusBall.stop();
programBonusNumber.text = bonusArray[0];
bonusBall.removeEventListener(Event.ENTER_FRAME, bonusBallstop);
showTimer.stop();
fadeAndSort();
}
}
if (count==0)
{
ball1.visible = true;
ball1.play();
var stopFrame1:int = 75;
ball1stop();
}
else if (count==1)
{
ball2.visible = true;
ball2.addEventListener(Event.ENTER_FRAME, ball2stop);
ball2.play();
var stopFrame2:int = 75;
ball2stop();
}
else if (count==2)
{
ball3.visible = true;
ball3.addEventListener(Event.ENTER_FRAME, ball3stop);
ball3.play();
var stopFrame3:int = 75;
ball3stop();
}
else if (count==3)
{
ball4.visible = true;
ball4.addEventListener(Event.ENTER_FRAME, ball4stop);
ball4.play();
var stopFrame4:int = 75;
ball4stop();
}
else if (count==4)
{
ball5.visible = true;
ball5.addEventListener(Event.ENTER_FRAME, ball5stop);
ball5.play();
var stopFrame5:int = 75;
}
else if (count==5)
{
ball6.visible = true;
ball6.addEventListener(Event.ENTER_FRAME, ball6stop);
ball6.play();
var stopFrame6:int = 75;
ball6stop();
}
else if (count==6)
{
bonusBall.visible = true;
bonusBall.addEventListener(Event.ENTER_FRAME, bonusBallstop);
bonusBall.play();
var stopFrame7:int = 75;
bonusballstop();
}
}
count++;
mainArray[0] = userNumber1.text;
mainArray[1] = userNumber2.text;
mainArray[2] = userNumber3.text;
mainArray[3] = userNumber4.text;
mainArray[4] = userNumber5.text;
mainArray[5] = userNumber6.text;
}
I bet this is the event he has problem with:
event.ENTER_FRAME
and should be Event.ENTER_FRAME
also I've found this in your "code"
function showBalls(Event:TimerEvent)
You need to put this line at the top of your file so flash knows where to look for the Event Class
You'll also need one for Timer, TimerEvent
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

Flash video in movieclip problems

I have several video files playing on the stage. I've converted them into movieclips so that I can scale and drag them by clicking. The problem is that I cannot loop them.
Then I tried to make them as SWF playback object's but after that my code wasn't working with them.
Next step was to make them Embedded video objects so that they loop automatically and the code is working. After that there appeared a problem that the objects are duplicating at some point.
Here's the original code as the videos are movieclips.
var allDraggables:Array = new Array();
var mouseHold = false;
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mUp(MouseEvent)
{
mouseHold = false;
}
function draggableObject(mc)
{
var mouseOnThisObject = false;
allDraggables.push(mc);
mc.addEventListener(Event.ENTER_FRAME, drag);
mc.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
function mDown(MouseEvent)
{
mouseHold = true;
mouseOnThisObject = true;
}
function drag(MouseEvent)
{
if (mouseHold == true && mouseOnThisObject == true)
{
mc.addEventListener(Event.ENTER_FRAME, dragger);
}
if (mouseHold == false)
{
mc.removeEventListener(Event.ENTER_FRAME, dragger);
mouseOnThisObject = false;
}
}
mc.doubleClickEnabled = true;
mc.addEventListener(MouseEvent.DOUBLE_CLICK, scaleMe);
function scaleMe(e:MouseEvent)
{
if (e.target.scaleX < 2)
{
e.target.scaleX= e.target.scaleY = 2;
}
else (e.target.scaleX= e.target.scaleY = 1);
}
function dragger(Event)
{
mc.x+=(mouseX-mc.x)/3;
mc.y+=(mouseY-mc.y)/3;
for (var i:int=0; i<allDraggables.length; i++){
if(mc.hitTestObject(allDraggables[i]) && getChildIndex(allDraggables[i]) > getChildIndex(mc)){
swapChildren(allDraggables[i], mc)
}
}
}
}
draggableObject(green);
draggableObject(red);
draggableObject(video1);
draggableObject(video2);
draggableObject(video3);
Well it's hard to tell what you've tried exactly, since you haven't provided any code (yet)..
However, from the top of my head, I think this will work:
if(videoMC1.currentFrame == 250) { //put the number of the last frame of the movieclip in place of 250
loopMC();
}
function loopMC() {
videoMC1.stop();
videoMC1.gotoAndPlay(1);
}
What you do here is simple; you check the current frame that is passed/playing and when it reaches the desired number (in your case most likely the last frame) it calls a function that resets and plays the video.
I found some old code I once used in a project, maybe you could try this. In my flash app it worked, although I didn't put the video into a movieclip.
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
var nc:NetConnection = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.bufferTime = 10;
var vid:Video = new Video(1024,640);
vid.attachNetStream(ns);
addChild(vid);
ns.addEventListener(NetStatusEvent.NET_STATUS, ns_onPlayStatus)
function ns_onPlayStatus(event:NetStatusEvent):void {//loop video
if(event.info.code == "NetStream.Play.Stop") {
ns.seek(0);
}
}

mp3 Looping Problems-Actionscript 3

This is so weird, this post wont let me start the sentence with hello hi or howdy maybe cache issue, anyways, hello, i'm very very new to AS3 so my knowledge is limited, hoping you pros can take it easy on the new guy :).
I'm building a flash site with an animation that loops and i've added an mp3 song as background music, it's also set to loop and auto play. the problem is, that the song is longer than the animation, and when the animation goes back to frame 1, the song overlaps the one that's still playing. I'd like for the song to fully finish before it starts playing again. yeaaah there's probably a heap of code missing, but i got closer to what i want, just need your help to polish it a little.
here is the code so far
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean=false;
var pausePosition:Number=0;
var myMusic:Sound=new Sound();
var soundFile:URLRequest=new URLRequest("Fluke.mp3");
myMusic.load(soundFile);
var channel:SoundChannel;
//if commands
if (AutoPlay==true) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
//pausebutton functions
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
play_btn.addEventListener(MouseEvent.CLICK,playMus ic);
function playMusic(event:MouseEvent):void
{ if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
}
THIS is the full working code thanks to Josha, with an .addEventListener that i added so that the song would loop after its finished playing.
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean;
var pausePosition:Number;
var myMusic:Sound;
var soundFile:URLRequest;
var channel:SoundChannel;
// only create music and register event listeners one time
if (myMusic == null)
{
soundFile = new URLRequest("Fluke.mp3");
myMusic = new Sound();
myMusic.load(soundFile);
pausePosition = 0;
if (AutoPlay)
{
channel = myMusic.play(pausePosition);
isPlaying = true;
}
else
{
isPlaying = false;
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
play_btn.addEventListener(MouseEvent.CLICK, playMusic);
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event) {
pausePosition = 0;
channel = myMusic.play(pausePosition);
isPlaying = true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
//playbutton functions
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
function playMusic(event:MouseEvent):void
{
if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
}
I can not place comments yet, so have to use answer.
Are you placing your code in separate AS files; or is this code placed at the first frame of your animation?
If the latter is the case, that indeed you will get overlapping sounds. Since the actionscript code is executed every time the playhead returns to frame 1; creating a new Sound object and starting to play that sound.
A simple solution is to create a key frame at the last frame and add the following action script:
gotoAndPlay(2);
This will loop your animation from frame 2, skipping over the action script.
Another solution that might work (you would have to test it):
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean;
var pausePosition:Number;
var myMusic:Sound;
var soundFile:URLRequest;
var channel:SoundChannel;
// only create music and register event listeners one time
if (myMusic == null)
{
soundFile = new URLRequest("Fluke.mp3");
myMusic = new Sound();
myMusic.load(soundFile);
pausePosition = 0;
if (AutoPlay)
{
channel = myMusic.play(pausePosition);
isPlaying = true;
}
else
{
isPlaying = false;
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
play_btn.addEventListener(MouseEvent.CLICK, playMusic);
}
//playbutton functions
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
function playMusic(event:MouseEvent):void
{
if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
}

hitTestObject collision detection not working in as3!

I am trying to create a platformer game and i am trying to make "player1" stop when it hits a "platform". here is my code so far,
gotoAndStop("gameStart");
import flash.display.MovieClip;
import flash.events.*;
import flash.ui.Keyboard;
import flash.ui.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
player1.gotoAndStop("nothing");
//private var speed:Number = 0;
//private var maxspeed:Number = 4;
var myTimer:Timer = new Timer(10,0);
stage.focus = this;
player1.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
/*
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
player1.y += 2;
}
*/
function setup() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, reactToArrowKeys);
}
setup();
function reactToArrowKeys(keyEvent:KeyboardEvent) {
if (keyEvent.keyCode == 37) {
if (player1.x > 0) {
player1.x -= 5;
}
} else if (keyEvent.keyCode == 39) {
if (player1.x < 700) {
player1.x += 5;
}
}
}
function enterFrameHandler(e:Event):void {
if (player1.hitTestObject(platform)) {
trace("hitting");
} else {
player1.y += 4;
}
}
however the hitTestObject function (enterFrameHandler) does not work properly and will always take the "else" route.
please help!
The code as posted works fine for me. I'd look for some other kind of silly mistake - for example, if you copied and pasted movie clips, you might have more than one clip on the stage named "platform", in which case your reference may not resolve to the one you intend. Or something else along those lines.
To track it down, try calling:
trace( player1.getBounds(stage) );
trace( platform.getBounds(stage) );
which will tell you where Flash thinks the bounding boxes of those clips are. My guess is that code will return something other than what you'd expect, and resolving that discrepancy will show where the bug is.