how to change width of movieclip inside sprite - actionscript-3

I really need your help... Below is my code for mp3 player. And the annoying problem is inside updateTime function, test.width doesn't want to change its value from 0, and that very strange behavior and I cannot understand why...
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.media.SoundLoaderContext;
import flash.display.Sprite;
import flash.events.ProgressEvent;
var playlist:XML = new XML();
var loader:URLLoader = new URLLoader();
var songNamok:String = new String();
var songPathok:String = new String();
var arrayUrl:Array = new Array();
var arrayNames:Array = new Array();
var track:Number = 0;
var myMusic:Sound = new Sound();
var soundFile:URLRequest = new URLRequest();
var channel:SoundChannel = new SoundChannel();
var sTransform:SoundTransform = new SoundTransform();
var pressedButton:Object = new Object();
var tf:TextFormat = new TextFormat();
var IsPausePressed:Number = 0;
tf.color = 0xFF0000;
txtLengthOfSong.setStyle("textFormat", tf);
var tfforSong:TextFormat = new TextFormat();
tfforSong.font = "Arial";
tfforSong.size = 16;
tfforSong.bold = true;
tfforSong.color = 0xFFffff;
movingString.setStyle("textFormat", tfforSong);
//Create test bar (progressBar) in this place, all childs are drawn objects,
//they have width and height
var container:Sprite = new Sprite();
addChild(container);
container.addChild(emptyProgress);
container.addChild(loadedProgress);
container.addChild(test);
container.addChild(transparentProgress);
container.setChildIndex(emptyProgress,0);
container.setChildIndex(loadedProgress,1);
container.setChildIndex(test, 2);
container.setChildIndex(transparentProgress,3);
var secondContainer:Sprite = new Sprite();
addChild(secondContainer);
secondContainer.addChild(emptyVolume);
secondContainer.addChild(filledVolume);
secondContainer.addChild(transparentVolume);
//In accordance to this timer test (progress bar) and
// remaining time will be changed
var myTimer:Timer = new Timer(50);
var myTimerForTremble = new Timer(5);
var songPosition:Number = 0;
var myContext:SoundLoaderContext = new SoundLoaderContext(5000);
var firstTime = 0;
buttonPlay.addEventListener (MouseEvent.CLICK, playMusic);
buttonStop.addEventListener (MouseEvent.CLICK, stopMusic);
buttonPause.addEventListener (MouseEvent.CLICK, pauseMusic);
buttonPlay.addEventListener (MouseEvent.MOUSE_OVER, startTimer);
buttonStop.addEventListener (MouseEvent.MOUSE_OVER, startTimer);
buttonPause.addEventListener (MouseEvent.MOUSE_OVER, startTimer);
transparentProgress.addEventListener(MouseEvent.CLICK, changePosition);
transparentVolume.addEventListener(MouseEvent.CLICK, changeVolume);
myTimer.addEventListener(TimerEvent.TIMER, updateTime);
myTimerForTremble.addEventListener (TimerEvent.TIMER, tremble);
buttonPlay.addEventListener (MouseEvent.MOUSE_OUT, stopTimer);
buttonStop.addEventListener (MouseEvent.MOUSE_OUT, stopTimer);
buttonPause.addEventListener (MouseEvent.MOUSE_OUT, stopTimer);
filledVolume.width = (channel.soundTransform.volume * 10)*0.98;
function progressOfDownloading (event:ProgressEvent):void{
var loadedMp3:Number = Math.round(100 * (event.bytesLoaded / event.bytesTotal));
loadedProgress.width = 35;
}
function songIsOver (evt:Event):void{
evt.currentTarget.removeEventListener(evt.type, songIsOver);
myTimer.stop();
firstTime = 0;
songPosition = 0;
try {
channel.stop();
myMusic.close();
} catch(e:Error){
channel.stop();
}
ExternalInterface.call("PlayNextSong","");
}
function playTheMusic ():void{
myTimer.stop();
firstTime = 0;
if (IsPausePressed == 0)
songPosition = 0;
try {
channel.stop();
myMusic.close();
} catch(e:Error){
channel.stop();
}
playMusic();
}
function changeVolume(evt:MouseEvent):void{
var x:Number = new Number();
x = evt.stageX;
var percentOfVolume:Number = new Number();
percentOfVolume = Math.floor((x-40)/0.98);
percentOfVolume = percentOfVolume / 10;
sTransform.volume = percentOfVolume;
channel.soundTransform = sTransform;
filledVolume.width = x - 40;
}
function changePosition (evt:MouseEvent):void{
if (firstTime == 1)
{
var x:Number = new Number();
x = evt.stageX;
var percent:Number = new Number();
var totalSeconds:Number = myMusic.length;
percent = myMusic.length/100 * ((x-22)/1.56);
songPosition = percent;
channel.stop();
channel = myMusic.play(songPosition);
channel.addEventListener(Event.SOUND_COMPLETE, songIsOver);
}
}
//This function will launch every 50 milliseconds, because of myTimer
function updateTime(evt:TimerEvent):void{
var progressok:String = new String(channel.position / myMusic.length);
var progressPercent:Number = new Number (channel.position / myMusic.length);
progressPercent = progressPercent * 100;
//this is the main problem: test.width doesn't want to change at all, however
// if I will write solid value like: 10, 12, 13 it works, and also when I write
// test.width = Math.random() * 100; it also works...
test.width = Math.round(progressPercent * 1.56);
var currentPosition:Number = new Number();
currentPosition = channel.position;
var timeRemain:Number = new Number();
timeRemain = myMusic.length - channel.position;
var totalSeconds:Number = timeRemain/1000;
var minutes:Number = Math.floor(totalSeconds/60);
var seconds = Math.floor(totalSeconds)%60;
if (seconds<10) {
seconds = "0"+seconds;
}
txtLengthOfSong.text = minutes+":"+seconds;
var randomHeight:Number = new Number();
randomHeight = Math.random()*100;
if (randomHeight > 55)
randomHeight = 55;
if (randomHeight < 0)
randomHeight = 0;
column1.height = randomHeight;
column2.height = randomHeight - (Math.random()*100);
column3.height = randomHeight / 3 * 2;
column4.height = randomHeight - (Math.random()*100);
column5.height = randomHeight - (Math.random()*100);
column6.height = randomHeight - (Math.random()*100);
column7.height = randomHeight - (Math.random()*100);
column8.height = randomHeight - (Math.random()*100);
column9.height = randomHeight - (Math.random()*100);
column10.height = randomHeight - (Math.random()*100);
if (movingString.textWidth > 150)
{
var deletedChar:String = new String();
var movingText:String = new String();
movingText = movingString.text;
deletedChar = movingText.charAt(0);
movingText = movingText.substr(1) + deletedChar;
movingString.text = movingText;
}
}
function stopTimer(evt:MouseEvent):void{
pressedButton.y = 145;
myTimerForTremble.stop();
}
function tremble(evt:TimerEvent):void{
pressedButton.y = 145 + ((Math.random()*10) - 5);
}
function startTimer (evt:MouseEvent):void{
if (!myTimerForTremble.running)
pressedButton = evt.target;
myTimerForTremble.start();
}
function pauseMusic(evt:MouseEvent = null):void{
IsPausePressed = 1;
songPosition = channel.position;
pressedButton.y = 145;
myTimerForTremble.stop();
channel.stop();
firstTime = 0;
myTimer.stop();
}
ExternalInterface.addCallback('pauseTheMusic', pauseMusic);
ExternalInterface.addCallback('resetSongPosition', resetSongPosition);
function resetSongPosition ():void{
songPosition = 0;
}
ExternalInterface.addCallback('select_track', SelectTrack);
function SelectTrack(songPath:String, songName:String):void{
songPathok = songPath;
songNamok = songName;
playTheMusic();
}
function playMusic (evt:MouseEvent = null):void{
if (firstTime == 0)
firstTime = 1;
else
return;
soundFile.url = songPathok;
movingString.text = songNamok + " ";
var nextTitle:Sound = new Sound(soundFile);
myMusic = nextTitle;
channel = myMusic.play(songPosition);
channel.addEventListener(Event.SOUND_COMPLETE, songIsOver);
pressedButton.y = 145;
myTimerForTremble.stop();
myTimer.start();
var percentOfVolume:Number = new Number();
percentOfVolume = Math.floor(filledVolume.width/0.98);
percentOfVolume = percentOfVolume / 10;
sTransform.volume = percentOfVolume;
channel.soundTransform = sTransform;
}
function stopMusic (evt:MouseEvent):void{
firstTime = 0;
pressedButton.y = 145;
myTimerForTremble.stop();
channel.stop();
songPosition = 0;
myTimer.stop();
}

Related

How do I remove eventlisteners and objects from an array upon game completion?

I have written a drag and drop game and, for the most part, it works.
It runs and does what I need it to do.
However, I cannot figure out two things.
How to remove the toy objects from the screen and re-add them after game over.
How to remove the event listeners for dragging/collision action after game over has initiated.
At the moment, after score is displayed you can still drop items in the toybox and make the score rise even when game over has been displayed.
Does anyone have any idea what I am doing wrong?
I would love to figure this out but it is driving me crazy.
Any help would be great.
Here is my code ...
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.Font;
import flash.filters.GlowFilter;
public class MainGame extends MovieClip {
const BG_SPEED:int = 5;
const BG_MIN:int = -550;
const BG_MAX:int = 0;
const PBG_SPEED:int = 3;
var bg:BackGround = new BackGround;
var paraBg:ParaBg = new ParaBg;
var toybox:TargetBox = new TargetBox;
var toy:Toy = new Toy;
var tryAgain:TryAgain = new TryAgain;
var cheer:Cheer = new Cheer;
var eightBit:EightBit = new EightBit;
var countDown:Number = 30;
var myTimer:Timer = new Timer(1000, 30);
var myText:TextField = new TextField;
var myText2:TextField = new TextField;
var myTextFormat:TextFormat = new TextFormat;
var myTextFormat2:TextFormat = new TextFormat;
var font1:Font1 = new Font1;
var kids:Kids = new Kids;
var count:int = 0;
var finalScore:int = 0;
var score:Number = 0;
var toy1:Toy1 = new Toy1;
var toy2:Toy2 = new Toy2;
var toy3:Toy3 = new Toy3;
var toy4:Toy4 = new Toy4;
var toy5:Toy5 = new Toy5;
var toy6:Toy6 = new Toy6;
var toy7:Toy7 = new Toy7;
var toy8:Toy8 = new Toy8;
var toy9:Toy9 = new Toy9;
var toy10:Toy10 = new Toy10;
var toy11:Toy11 = new Toy11;
var toy12:Toy12 = new Toy12;
var toy13:Toy13 = new Toy13;
var toy14:Toy14 = new Toy14;
var toy15:Toy15 = new Toy15;
var toy16:Toy16 = new Toy16;
var toy17:Toy17 = new Toy17;
var toy18:Toy18 = new Toy18;
var toy19:Toy19 = new Toy19;
var toy20:Toy20 = new Toy20;
var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);
public function mainGame():void
{
trace("HI");
eightBit.play(0, 9999);
addChildAt(paraBg, 0);
addChildAt(bg, 1);
addChildAt(kids, 2);
kids.x = 310;
kids.y = 200;
addChild(toy);
toy.x = 306;
toy.y = 133;
addChild(toybox);
toybox.x = 295;
toybox.y = 90;
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
for (var i:int = 0; i < toyArray.length; i++)
{
addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);
}
}
public function bgScroll (e:Event)
{
stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.start();
e.target.addEventListener(Event.ENTER_FRAME, collision);
if (stage.mouseX > 600 && bg.x > BG_MIN)
{
bg.x -= BG_SPEED;
paraBg.x -= PBG_SPEED;
for (var m:int=0; m< toyArray.length; m++)
{
(toyArray[m] as MovieClip).x -=BG_SPEED
}
}
else if (stage.mouseX < 50 && bg.x < BG_MAX)
{
bg.x += BG_SPEED;
paraBg.x += PBG_SPEED;
for (var j:int=0; j< toyArray.length; j++)
{
(toyArray[j] as MovieClip).x +=BG_SPEED
}
}
for (var k:int = 0; k < toyArray.length; k++)
{
toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);
}
bg.addEventListener(Event.ENTER_FRAME, bgScroll);
} // End of BGScroll
public function collision (e:Event)
{
for (var l:int=0; l< toyArray.length; l++)
{
if (toyArray[l].hitTestObject(toy))
{
removeChild(toyArray[l]);
toyArray[l].x=100000;
toybox.gotoAndPlay(2);
cheer.play(1, 1);
score = score + 10;
trace(score);
}
if (score == 200)
{
timerDone();
myTimer.stop();
}
}
}
public function arrayGrab(e:MouseEvent)
{
e.target.startDrag();
}
public function arrayDrop(e:MouseEvent)
{
stopDrag();
}
public function resetGame(e:Event):void {
trace("CLICK");
countDown = 30;
myText.text = "0" + countDown.toString();
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.reset();
removeChild(tryAgain);
myText2.visible = false;
score = 0;
}
public function countdown(e:TimerEvent):void
{
if (countDown > 0)
{
countDown--;
}
if (countDown < 10)
{
myText.text = "0" + countDown.toString();
myText.x = 270;
displayText();
}
else if (countDown < 20 && countDown > 9)
{
myText.text = countDown.toString();
myText.x = 280;
displayText();
}
else
{
myText.text = countDown.toString();
myText.x = 270;
displayText();
}
} // end of countdown function
public function displayText():void
{
myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 50, myText.y = 10;
myTextFormat.size = 50, myTextFormat.font = font1.fontName;
myText.setTextFormat(myTextFormat);
}
public function displayText2():void
{ myText2.filters = [new GlowFilter(0xFF0000, 1.0, 5, 5, 4)];
addChild(myText2);
myText2.width = 500, myText2.height = 35, myText2.x = 204, myText2.y = 200;
myTextFormat2.size = 30, myTextFormat2.font = font1.fontName;
myText2.setTextFormat(myTextFormat2);
}
public function timerDone(e:TimerEvent=null):void
{
if (countDown == 0)
{
count = 0;
finalScore = score;
}
else
{
count = (30) - (myTimer.currentCount);
finalScore = (count * 10) + (score);
}
myText.text = "GAME OVER!";
myText.x = 195;
displayText();
myText2.text = "Your score = " + (finalScore);
displayText2();
addChild(tryAgain);
tryAgain.x = 300;
tryAgain.y = 300;
tryAgain.addEventListener(MouseEvent.CLICK, resetGame);
}
} // End of class
} //End of package
Nevermind ... solved it.
Easiest was was to change
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
To
function addToys(xpos:int, ypos:int)
{
stage.addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
And then, in the timerDone function I added thisif statement ...
for (var m = 0; m < toyArray.length; m++) {
if (stage.contains(toyArray[m])) {
stage.removeChild(toyArray[m]);
}
Worked a treat!

How to get indexof a Bitmap image?

I am having a hard time to get an index of a bitmap image. I am not sure how should i suppose to do it.
What i am trying to do is:
1.)Loop a URLRequest and load bitmap pictures.
2.)Put them in individual _contentHolder
3.)Put everything in a viewport
4.)Check the index of the image when its clicked
Thanks for your time
Code
public var _contentHolder:Sprite = new Sprite;
public var _contentHolder1:Sprite;
public var loadedArray:Array = new Array;
public var blackBox:Sprite = new Sprite();
private var somedata:Array;
protected var Holder:Listing9 = new Listing9;
public var viewport:Viewport = new Viewport();
public var scroller:TouchScroller = new TouchScroller();
var my_url:Array = somedata;
function loadImage():void
{
somedata = SearchVectorTest.lists;
for (var i:int = 5; i < somedata.length; i++)
{
if (somedata[i])
{
var loader:Loader = new Loader();
loader.load(new URLRequest("http://www.rentaid.info/rent/" + somedata[i]));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}
}
}
function onImageLoaded(e:Event):void
{
loadedArray.push(e.currentTarget.loader.content as Bitmap);
for (var i:int = 0; i < loadedArray.length; i++)
{
var currentY1:int = 200;
e.currentTarget.loader.content.height = 200;
e.currentTarget.loader.content.y += currentY1;
currentY1 += e.currentTarget.loader.content.height + 300;
_contentHolder.mouseChildren = false; // ignore children mouseEvents
_contentHolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
_contentHolder.useHandCursor = true; // add hand cursor on mouse over
_contentHolder.buttonMode = true;
_contentHolder.addChild(loadedArray[i]);
}
var viewport:Viewport = new Viewport();
viewport.y = 0;
viewport.addChild(_contentHolder);
var scroller:TouchScroller = new TouchScroller();
scroller.width = 300;
scroller.height = 265;
scroller.x = 10;
scroller.y = 100;
scroller.viewport = viewport;
addChild(scroller);
_contentHolder.addEventListener(MouseEvent.CLICK, gotoscene);
}
loadImage();
public function gotoscene(e:MouseEvent):void
{
BitmapArray.push(loadedArray);
var index:Number;
index = BitmapArray.indexOf(e.target);
trace(index);
trace(_contentHolder);
trace(_contentHolder.parent);
blackBox.graphics.beginFill(0x000000);
blackBox.graphics.drawRect(-1, -1, stage.width, stage.height);
blackBox.alpha = 0.7;
addChild(blackBox);
Holder.height = 300;
Holder.width = stage.width;
Holder.x = 0;
Holder.y = 100;
trace(blackBox);
trace(blackBox.parent);
addChild(Holder);
}
function gotoscene1(e:MouseEvent):void
{
removeChild(Holder);
removeChild(blackBox);
}

Netstream audio playing twice

I am new to flash (this is the first time I've ever used it or actionscript) and I'm trying to make a video player. The video player gets params from the embed code and pulls the videos from a folder on the server.
I've got the following code (I've removed everything that I'm 100% sure isn't causing my problem):
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.events.NetStatusEvent;
import flash.events.MouseEvent;
import flash.events.FullScreenEvent;
import flash.events.Event;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextFormat;
import flash.media.SoundTransform;
var nc:NetConnection;
var ns:NetStream;
var ns2:NetStream;
var video:Video;
var video2:Video;
//get filename parameters from embed code
var filename:String = root.loaderInfo.parameters.filename;
var filename2:String = root.loaderInfo.parameters.filename2;
var t:Timer = new Timer(5000);
var duration;
var currentPosition:Number;
var st:Number;
var started:Boolean;
Object(this).mcPlay.buttonMode = true;
Object(this).mcPause.buttonMode = true;
Object(this).ScreenClick.buttonMode = true;
Object(this).mcMax.buttonMode = true;
Object(this).mcSwitcher.buttonMode = true;
Object(this).mcPause.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).mcPlay.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).ScreenClick.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).mcMax.addEventListener(MouseEvent.CLICK,Maximize);
Object(this).slideVolume.addEventListener(Event.CHANGE, ChangeVolume);
Object(this).mcSwitcher.addEventListener(MouseEvent.CLICK, ToggleSwitcher);
t.addEventListener(TimerEvent.TIMER, TimerComplete);
stage.addEventListener(MouseEvent.MOUSE_MOVE, resetTimer);
stage.addEventListener(MouseEvent.MOUSE_DOWN, resetTimer);
stage.addEventListener(MouseEvent.MOUSE_UP, resetTimer);
stage.addEventListener(Event.ENTER_FRAME, videoTimer);
if (!nc) start();
var IsPaused:String;
function start():void
{
Object(this).slideVolume.maximum = 100;
Object(this).slideVolume.value = 100;
started = false;
var tf:TextFormat = new TextFormat();
tf.color = 0xFFFFFF;
tf.bold = true;
this.lblTime.setStyle("textFormat", tf);
connect();
t.start();
}
function connect():void
{
nc = new NetConnection();
nc.client = this;
nc.addEventListener(NetStatusEvent.NET_STATUS, OnNetStatus);
nc.connect(null);
}
function OnNetStatus(e:NetStatusEvent):void
{
switch(e.info.code)
{
case "NetConnection.Connect.Success":
if (!started)
{
started = true;
stream();
}
else
{
finish();
}
break;
default:
finish();
break;
}
}
function stream():void
{
ns = new NetStream(nc);
ns.client = this;
ns.bufferTime = 5; // set the buffer time to 5 seconds
if ((filename2 != null) && (filename2.length > 0))
{
ns2 = new NetStream(nc)
//ns2.client = this; //Uncomment to use ns2 vid for duration info
ns2.bufferTime = 5; // set the buffer time to 5 seconds
startVideo(2);
currentPosition = 1; //Default
ns.seek(0);
ns2.seek(0);
}
else
{
this.mcSwitcher.visible = false;
startVideo(1);
ns.seek(0);
}
}
function startVideo(num:Number):void
{
var startVolume:SoundTransform = new SoundTransform();
startVolume.volume = slideVolume.value / 100;
if (num == 2)
{
video = new Video(320,180);
video.x = 0;
video.y = 90;
addChild(video);
video.attachNetStream(ns);
ns.checkPolicyFile = false;
ns.play(filename); //path/filename
setChildIndex(video,1);
video2 = new Video(320,180);
video2.x = 320;
video2.y = 90;
addChild(video2);
video2.attachNetStream(ns2);
ns2.checkPolicyFile = false;
ns2.play(filename2); //path/filename
setChildIndex(video2,1);
ns.soundTransform = startVolume;
var videoVolumeTransform2:SoundTransform = new SoundTransform();
videoVolumeTransform2.volume = 0;
ns2.soundTransform = videoVolumeTransform2;
ns2.receiveAudio(false);
}
else if (num == 1)
{
video = new Video(640,360);
video.x = 0;
video.y = 0;
addChild(video);
video.attachNetStream(ns);
ns.checkPolicyFile = false;
ns.play(filename); //path/filename
setChildIndex(video,1);
ns.soundTransform = startVolume;
}
IsPaused = "playing";
this.removeChild(mcPlay);
setChildIndex(this.ScreenClick,0);
setChildIndex(this.mcTitleOverlay,2);
}
function ShowControls ():void
{
for (var i:int = 0; i < Object(root).numChildren; i++)
{
switch (Object(root).getChildAt(i))
{
case mcPause:
if (IsPaused != "paused")
Object(root).getChildAt(i).visible = true;
break;
case mcPlay:
if (IsPaused != "playing")
Object(root).getChildAt(i).visible = true;
break;
case mcSwitcher:
if ((filename2 != null) && (filename2.length > 0))
Object(root).getChildAt(i).visible = true;
break;
default:
Object(root).getChildAt(i).visible = true; //Bring back everything else
break;
}
ScreenClick.y = 154;
}
}
function videoTimer(e:Event):void
{
var curTime = ns.time; //Current time in seconds
var curMinutes = Math.floor(curTime / 60); //Get the minutes
var curSeconds = Math.floor(curTime % 60); //Get the leftover seconds
var durMinutes = Math.floor(duration / 60);
var durSeconds = Math.floor(duration % 60);
//Add the zeroes to the begining of the seconds if it is needed.
if (curSeconds < 10)
curSeconds = "0" + curSeconds;
if (durSeconds < 10)
durSeconds = "0" + durSeconds;
Object(this).lblTime.text = curMinutes + ":" + curSeconds + " / " + durMinutes + ":" + durSeconds;
}
function PlayPause (e:MouseEvent):void
{
switch (IsPaused)
{
case "playing":
IsPaused = "paused";
this.mcPlay.visible = true;
this.mcPause.visible = false;
ns.togglePause();
ns2.togglePause();
break;
case "paused":
IsPaused = "playing";
this.mcPause.visible = true;
this.mcPlay.visible = false;
ns.togglePause();
ns2.togglePause();
break;
default:
//
break;
}
}
The problem I have is small but frustrating (I've spend most of today trying to figure it out with zero progress made). It is thus: Everything works perfectly, except that when the videos load up and play, sound plays twice (for the video that has sound enabled). I am at my wits end trying to figure this out, any help would be greatly appreciated!
Thanks!
EDIT:
Ok, on further research (re-writing every function very simply and seeing if the problem goes away with the features) I've determined that the following function is the root of all evil (or at least my problems):
function startVideo(num:Number):void
{
var startVolume:SoundTransform = new SoundTransform();
startVolume.volume = Object(this).slideVolume.sldVol.value / 100;
if (num == 2)
{
video = new Video(320,180);
video.x = 0;
video.y = 90;
addChild(video);
video.attachNetStream(ns);
ns.checkPolicyFile = false;
ns.play(filename); //path/filename
this.removeChild(btnPlay);
setChildIndex(video,1);
video2 = new Video(320,180);
video2.x = 320;
video2.y = 90;
addChild(video2);
video2.attachNetStream(ns2);
ns2.checkPolicyFile = false;
ns2.play("test.mp4"); //path/filename
setChildIndex(video2,1);
ns.soundTransform = startVolume;
var videoVolumeTransform2:SoundTransform = new SoundTransform();
videoVolumeTransform2.volume = 0;
ns2.soundTransform = videoVolumeTransform2;
ns2.receiveAudio(false);
}
else if (num == 1)
{
video = new Video(640,360);
video.x = 0;
video.y = 0;
addChild(video);
video.attachNetStream(ns);
ns.checkPolicyFile = false;
ns.play("test.flv"); //path/filename
setChildIndex(video,1);
ns.soundTransform = startVolume;
}
IsPaused = "playing";
this.removeChild(btnPlay);
setChildIndex(this.ScreenClick,0);
//setChildIndex(this.mcTitleOverlay,2);
}
I shall persevere with my troubleshooting (I've isolated the problem, hopefully the next step is a solution!

Create multiple shapes dynamically in ActionScript 3

This is code that I use. I am trying to dynamically create multiple shapes and create animation (function drawLine) with them. I have result shows me only one animation(first one) and then nothing. It should be 6 animation. I think that addChild() function is not creating multiple shapes as I wanted.
import flash.display.Shape;
var startPoint:Point = new Point();
var endPoint:Point = new Point();
var prog:Number = 0;
var bonus:Number = 1;
var frames:int = 150;
var numbers:Array = new Array();
numbers[0] = new Array();
numbers[0][0] = 460;
numbers[0][1] = 100;
numbers[1] = new Array();
numbers[1][0] = 10;
numbers[1][1] = 20;
numbers[2] = new Array();
numbers[2][0] = 10;
numbers[2][1] = 100;
numbers[3] = new Array();
numbers[3][0] = 10;
numbers[3][1] = 180;
numbers[4] = new Array();
numbers[4][0] = 160;
numbers[4][1] = 20;
numbers[5] = new Array();
numbers[5][0] = 160;
numbers[5][1] = 100;
numbers[6] = new Array();
numbers[6][0] = 160;
numbers[6][1] = 180;
numbers[7] = new Array();
numbers[7][0] = 310;
numbers[7][1] = 20;
numbers[8] = new Array();
numbers[8][0] = 310;
numbers[8][1] = 100;
numbers[9] = new Array();
numbers[9][0] = 310;
numbers[9][1] = 180;
var fullDate:String = "271524";
var i:Number;
var pom:Number;
var shapes:Vector.<Shape> = new Vector.<Shape>();
for (i=0; i < fullDate.length; i++){
pom = int(fullDate.charAt(i));
shapes[i] = new Shape();
addChild(shapes[i]);
trace(numbers[pom+1][0]);
drawLine(numbers[pom][0], numbers[pom][1], numbers[pom+1][0], numbers[pom+1][1], 120);
}
function drawLine(startX:Number, startY:Number, endX:Number, endY:Number, time:Number = 120):void {
startPoint.x = startX;
startPoint.y = startY;
endPoint.x = endX;
endPoint.y = endY;
frames = time;
prog = 0;
this.addEventListener(Event.ENTER_FRAME, tick);
}
function drawLineTick(progress:Number):void{
for each(var shape:Shape in shapes)
{
shape.graphics.clear();
shape.graphics.lineStyle(14,0x2dfdfd);
shape.graphics.moveTo(startPoint.x,startPoint.y);
shape.graphics.lineTo(startPoint.x + ((endPoint.x - startPoint.x) * progress), startPoint.y + ((endPoint.y - startPoint.y) * progress));
}
}
function tick(e:Event):void {
if(prog >= frames){
this.removeEventListener(Event.ENTER_FRAME, tick);
}
drawLineTick(prog / frames);
prog += bonus;
}
The problem is, s only refers to the last Shape you created. Try something like this: (at the moment, all the shapes are on top of each, so you won't see multiple animations, but you can adjust that)
fullDate = "174689";
var shapes:Vector.<Shape> = new Vector.<Shape>();
for (i=0; i < fullDate.length; i++){
pom = int(fullDate.charAt(i));
shapes[i] = new Shape();
addChild(shapes[i]);
drawLine(numbers[pom][0], numbers[pom][1], numbers[pom+1][0], numbers[pom+1][1], 50);
}
function drawLine(startX:Number, startY:Number, endX:Number, endY:Number, time:Number = 120):void {
startPoint.x = startX;
startPoint.y = startY;
endPoint.x = endX;
endPoint.y = endY;
frames = time;
prog = 0;
this.addEventListener(Event.ENTER_FRAME, tick);
}
function drawLineTick(progress:Number):void{
for each(var shape:Shape in shapes)
{
shape.graphics.clear();
shape.graphics.lineStyle(14,0x2dfdfd);
shape.graphics.moveTo(startPoint.x,startPoint.y);
shape.graphics.lineTo(startPoint.x + ((endPoint.x - startPoint.x) * progress), startPoint.y + ((endPoint.y - startPoint.y) * progress));
}
}
function tick(e:Event):void {
if(prog >= frames){
this.removeEventListener(Event.ENTER_FRAME, tick);
}
drawLineTick(prog / frames);
prog += bonus;
}

doubt regarding carrying data in custom events using actionscript

I am working on actionscript to generate a SWF dynamically using JSON data coming from an HTTP request. I receive the data on creationComplete and try to generate a tree like structure. I don’t create the whole tree at the same time. I create 2 levels, level 1 and level 2. My goal is to attach custom events on the panels which represent tree nodes. When users click the panels, it dispatches custom events and try to generate the next level. So, it goes like this :
On creation complete -> get JSON-> create top tow levels -> click on level 2-> create the level 2 and level 3 -> click on level 3-> create level 3 and 4. …and so on and so on. I am attaching my code with this email. Please take a look at it and if you have any hints on how you would do this if you need to paint a tree having total level number = “n” where n = 0 to 100
Should I carry the data around in CustomPageClickEvent class.
[code]
import com.iwobanas.effects.*;
import flash.events.MouseEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.BitmapFilterType;
import flash.filters.GradientGlowFilter;
import mx.controls.Alert;
private var roundedMask:Sprite;
private var panel:NewPanel;
public var oldPanelIds:Array = new Array();
public var pages:Array = new Array();
public var delPages:Array = new Array();
public function DrawPlaybook(pos:Number,title:String,chld:Object):void {
panel = new NewPanel(chld);
panel.title = title;
panel.name=title;
panel.width = 100;
panel.height = 80;
panel.x=pos+5;
panel.y=40;
var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
gradientGlow.distance = 0;
gradientGlow.angle = 45;
gradientGlow.colors = [0xFFFFF0, 0xFFFFFF];
gradientGlow.alphas = [0, 1];
gradientGlow.ratios = [0, 255];
gradientGlow.blurX = 10;
gradientGlow.blurY = 10;
gradientGlow.strength = 2;
gradientGlow.quality = BitmapFilterQuality.HIGH;
gradientGlow.type = BitmapFilterType.OUTER;
panel.filters =[gradientGlow];
this.rawChildren.addChild(panel);
pages.push(panel);
panel.addEventListener(MouseEvent.CLICK,
function(e:MouseEvent){onClickHandler(e,title,chld)});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
}
public function onClickHandler(e:MouseEvent,title:String,chld:Object):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPanelClicked(e:CustomPageClickEvent,title:String):void {
Alert.show("onCustomPanelClicked" + title);
var panel:NewPanel;
for each(var stp:NewPanel in pages){
startAnimation(e,stp);
}
if(title == e.panelClicked.title){
panel = new NewPanel(null);
panel.title = title;
panel.name=title;
panel.width = 150;
panel.height = 80;
panel.x=100;
panel.y=40;
this.rawChildren.addChild(panel);
var slideRight:SlideRight = new SlideRight();
slideRight.target=panel;
slideRight.duration=750;
slideRight.showTarget=true;
slideRight.play();
var jsonData = this.map.getValue(title);
var posX:Number = 50;
var posY:Number = 175;
for each ( var pnl:NewPanel in pages){
pages.pop();
}
for each ( var stp1:Object in jsonData.children){
panel = new NewPanel(null);
panel.title = stp1.text;
panel.name=stp1.id;
panel.width = 100;
panel.id=stp1.id;
panel.height = 80;
panel.x = posX;
panel.y=posY;
posX+=150;
var s:String="hi" + stp1.text;
panel.addEventListener(MouseEvent.CLICK,
function(e:MouseEvent){onChildClick(e,s);});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPnlClicked(e)});
this.rawChildren.addChild(panel);
pages.push(panel);
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
var slide:SlideUp = new SlideUp();
slide.target=panel;
slide.duration=1500;
slide.showTarget=false;
slide.play();
}
}
}
public function onChildClick(e:MouseEvent,s:String):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==e.currentTarget.title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPnlClicked(e:CustomPageClickEvent):void {
for each ( var pnl:NewPanel in pages){
pages.pop();
}
}
private function fadePanel(event:Event,panel:NewPanel):void{
panel.alpha -= .005;
if (panel.alpha <= 0){
panel.removeEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel);});
};
panel.title="";
}
private function startAnimation(event:CustomPageClickEvent,panel:NewPanel):void{
panel.addEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel)});
}
[/code]
Thanks in advance.
Palash
completely forgot i don't have enough rep to edit...
import com.iwobanas.effects.*;
import flash.events.MouseEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.BitmapFilterType;
import flash.filters.GradientGlowFilter;
import mx.controls.Alert;
private var roundedMask:Sprite;
private var panel:NewPanel;
public var oldPanelIds:Array = new Array();
public var pages:Array = new Array();
public var delPages:Array = new Array();
public function DrawPlaybook(pos:Number,title:String,chld:Object):void {
panel = new NewPanel(chld);
panel.title = title;
panel.name=title;
panel.width = 100;
panel.height = 80;
panel.x=pos+5;
panel.y=40;
var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
gradientGlow.distance = 0;
gradientGlow.angle = 45;
gradientGlow.colors = [0xFFFFF0, 0xFFFFFF];
gradientGlow.alphas = [0, 1];
gradientGlow.ratios = [0, 255];
gradientGlow.blurX = 10;
gradientGlow.blurY = 10;
gradientGlow.strength = 2;
gradientGlow.quality = BitmapFilterQuality.HIGH;
gradientGlow.type = BitmapFilterType.OUTER;
panel.filters = [gradientGlow];
this.rawChildren.addChild(panel);
pages.push(panel);
panel.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){onClickHandler(e,title,chld)});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
}
public function onClickHandler(e:MouseEvent,title:String,chld:Object):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPanelClicked(e:CustomPageClickEvent,title:String):void {
Alert.show("onCustomPanelClicked" + title);
var panel:NewPanel;
for each(var stp:NewPanel in pages){
startAnimation(e,stp);
}
if(title == e.panelClicked.title){
panel = new NewPanel(null);
panel.title = title;
panel.name=title;
panel.width = 150;
panel.height = 80;
panel.x=100;
panel.y=40;
this.rawChildren.addChild(panel);
var slideRight:SlideRight = new SlideRight();
slideRight.target=panel;
slideRight.duration=750;
slideRight.showTarget=true;
slideRight.play();
var jsonData = this.map.getValue(title);
var posX:Number = 50;
var posY:Number = 175;
for each ( var pnl:NewPanel in pages){
pages.pop();
}
for each ( var stp1:Object in jsonData.children){
panel = new NewPanel(null);
panel.title = stp1.text;
panel.name=stp1.id;
panel.width = 100;
panel.id=stp1.id;
panel.height = 80;
panel.x = posX;
panel.y=posY;
posX += 150;
var s:String="hi" + stp1.text;
panel.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){onChildClick(e,s);});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPnlClicked(e)});
this.rawChildren.addChild(panel);
pages.push(panel);
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
var slide:SlideUp = new SlideUp();
slide.target=panel;
slide.duration=1500;
slide.showTarget=false;
slide.play();
}
}
}
public function onChildClick(e:MouseEvent,s:String):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==e.currentTarget.title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPnlClicked(e:CustomPageClickEvent):void {
for each ( var pnl:NewPanel in pages){
pages.pop();
}
}
private function fadePanel(event:Event,panel:NewPanel):void{
panel.alpha -= .005;
if (panel.alpha <= 0){
panel.removeEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel);});
};
panel.title="";
}
private function startAnimation(event:CustomPageClickEvent,panel:NewPanel):void{
panel.addEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel)});
}