flash action-script 3 save and load - actionscript-3

Hey Guys I'm working on a game on flash action script 3 and i reached with my code to the point that i have already added a mute button which works perfectly but what i want to do is to keep it muted while in the game even if i closed the game re open it.
one another thing is that i want the game to be saved even if i closed it and re opened it exactly the way i left it.
so please help me guys here is my code below
var foundObjects:Number=0
var triesNumber:Number=0
stop()
import flash.net.SharedObject;
var mySo:SharedObject = SharedObject.getLocal("squaresGame");
if(mySo.data.levelNumber==null)
mySo.data.levelNumber="0.01"
//mySo.data.levelNumber
var level:Number=mySo.data.levelNumber
var sTransform1:SoundTransform = new SoundTransform(1,0);
function setMute1(vol){
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
}
levelText.text=(level*100).toString()
var turkeyArray:Array = new Array();
turkeyArray[1] = m1;
turkeyArray[2] = m2;
turkeyArray[3] = m3;
turkeyArray[4] = m4;
turkeyArray[5] = m5;
turkeyArray[6] = m6;
turkeyArray[7] = m7;
turkeyArray[8] = m8;
turkeyArray[9] = m9;
m1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
m2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
m3.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
m4.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
m5.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
m6.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
m7.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
m8.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
m9.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
event.currentTarget.play()
testFoundObject(event.currentTarget)
}
function testFoundObject(mc:Object):void
{
if(mc.hitTestObject(apple1) || mc.hitTestObject(apple2)||mc.hitTestObject(apple3))
{
foundObjects++
applesquresound.play()
}
else
emptysquresound.play()
trace(foundObjects)
triesNumber++
if(triesNumber>=3)
if(foundObjects>=3)
{
level+=0.01
mySo.data.levelNumber=level.toFixed(2)
levelText.text=(level*100).toString()
yes.play()
}
else{
no.play()
}
}
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
function fl_EnterFrameHandler(event:Event):void
{
if(m1.alpha<1)
for(var counter:Number=1;counter<=9;counter++){
MovieClip(turkeyArray[counter]).alpha+=level
}
}
button_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
gotoAndPlay(1)
}
toggleMuteBtn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
if(Mute1 == false) {
Mute1 = true;
setMute1(1);
toggleMuteBtn.gotoAndStop(1)
} else {
Mute1 = false;
setMute1(0);
toggleMuteBtn.gotoAndStop(2)
}
}
var locationMC:MovieClip=MovieClip(turkeyArray[randomRange(1,9)]);
apple1.x=locationMC.x
apple1.y=locationMC.y
locationMC=MovieClip(turkeyArray[randomRange(1,9)]);
apple2.x=locationMC.x
apple2.y=locationMC.y
while(apple2.hitTestObject(apple1) || apple2.hitTestObject(apple3))
{locationMC=MovieClip(turkeyArray[randomRange(1,9)]);
apple2.x=locationMC.x
apple2.y=locationMC.y
}
locationMC=MovieClip(turkeyArray[randomRange(1,9)]);
apple3.x=locationMC.x
apple3.y=locationMC.y
while(apple3.hitTestObject(apple1) || apple3.hitTestObject(apple2))
{locationMC=MovieClip(turkeyArray[randomRange(1,9)]);
apple3.x=locationMC.x
apple3.y=locationMC.y
}
function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}

You can use SharedObject class to store and load information about sound muting. As I see in your code, you already using this class for storing levelNumber, so I believe its not a challenge for you.

Related

how to get instance name of a movieClip dynamically?

I have movieClips (suppose= A) on stage. i need to modify them when i drop a moviclip (suppose= B) in a movieclip (suppose= C).
My code is
var stX:Number;
var stY:Number;
Ant.addEventListener(MouseEvent.MOUSE_DOWN, Objdrag);
Ant.addEventListener(MouseEvent.MOUSE_UP, ObjdropinPenta);
function Objdrag(e:MouseEvent):void
{
stX = e.currentTarget.x;
stY = e.currentTarget.y;
e.currentTarget.startDrag();
}
function ObjdropinPenta(e:MouseEvent):void
{
e.currentTarget.stopDrag();
if (e.currentTarget.hitTestObject(penta))
{
trace(e.currentTarget.name);
var mcName:
mcName = "S1" + e.currentTarget.name
trace(mcName.name);
mcName.visible = false;
e.currentTarget.visible = false;
}
else
{
e.currentTarget.x = stX;
e.currentTarget.y = stY;
}
}
I know how to make it separately but i want to make it dynamically
mcName.visible = false;
this is not working
where i am missing? Please guide
Thanks in advance

How to make a movie clip visible if only five movie clips (not more) are clicked

I have 25 movie clips on stage and they all can be clicked and colored. I want a movie clip named text_mc to became visible if only 5 specific buttons from those are clicked and colored - not more. If the user choose more than those five movie clips (even thought that 5 movie clips are included) then the movie clip named text_mc should stay invisible. I can' t do the last part: if more than those 5 specific movie clips are clicked then the text_mc should stay invisible. Can you please help me? This is my code
stop();
import flash.display.MovieClip;
var sximata:MovieClip = square1;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
text_mc.visible=false;
square1.addEventListener(MouseEvent.CLICK, onsquare1);
function onsquare1(e:MouseEvent):void {
sximata = square1;
}
square2.addEventListener(MouseEvent.CLICK, onsquare2);
function onsquare2(e:MouseEvent):void {
sximata = square2;
}
square3.addEventListener(MouseEvent.CLICK, onsquare3);
function onsquare3(e:MouseEvent):void {
sximata = square3;
}
square4.addEventListener(MouseEvent.CLICK, onsquare4);
function onsquare4(e:MouseEvent):void {
sximata = square4;
}
square5.addEventListener(MouseEvent.CLICK, onsquare5);
function onsquare5(e:MouseEvent):void {
sximata = square5;
}
square6.addEventListener(MouseEvent.CLICK, onsquare6);
function onsquare6(e:MouseEvent):void {
sximata = square6;
}
square7.addEventListener(MouseEvent.CLICK, onsquare7);
function onsquare7(e:MouseEvent):void {
sximata = square7;
}
square8.addEventListener(MouseEvent.CLICK, onsquare8);
function onsquare8(e:MouseEvent):void {
sximata = square8;
square8Clicked = true;
checkButtons();
}
square9.addEventListener(MouseEvent.CLICK, onsquare9);
function onsquare9(e:MouseEvent):void {
sximata = square9;
square9Clicked = true;
checkButtons();
}
square10.addEventListener(MouseEvent.CLICK, onsquare10);
function onsquare10(e:MouseEvent):void {
sximata = square10;
square10Clicked = true;
checkButtons();
}
square11.addEventListener(MouseEvent.CLICK, onsquare11);
function onsquare11(e:MouseEvent):void {
sximata = square11;
}
square12.addEventListener(MouseEvent.CLICK, onsquare12);
function onsquare12(e:MouseEvent):void {
sximata = square12;
}
square13.addEventListener(MouseEvent.CLICK, onsquare13);
function onsquare13(e:MouseEvent):void {
sximata = square13;
square13Clicked = true;
checkButtons();
}
square14.addEventListener(MouseEvent.CLICK, onsquare14);
function onsquare14(e:MouseEvent):void {
sximata = square14;
square14Clicked = true;
checkButtons();
}
square15.addEventListener(MouseEvent.CLICK, onsquare15);
function onsquare15(e:MouseEvent):void {
sximata = square15;
}
square16.addEventListener(MouseEvent.CLICK, onsquare16);
function onsquare16(e:MouseEvent):void {
sximata = square16;
}
square17.addEventListener(MouseEvent.CLICK, onsquare17);
function onsquare17(e:MouseEvent):void {
sximata = square17;
}
square18.addEventListener(MouseEvent.CLICK, onsquare18);
function onsquare18(e:MouseEvent):void {
sximata = square18;
}
square19.addEventListener(MouseEvent.CLICK, onsquare19);
function onsquare19(e:MouseEvent):void {
sximata = square19;
}
square20.addEventListener(MouseEvent.CLICK, onsquare20);
function onsquare20(e:MouseEvent):void {
sximata = square20;
}
square21.addEventListener(MouseEvent.CLICK, onsquare21);
function onsquare21(e:MouseEvent):void {
sximata = square21;
}
square22.addEventListener(MouseEvent.CLICK, onsquare22);
function onsquare22(e:MouseEvent):void {
sximata = square22;
}
square23.addEventListener(MouseEvent.CLICK, onsquare23);
function onsquare23(e:MouseEvent):void {
sximata = square23;
}
square24.addEventListener(MouseEvent.CLICK, onsquare24);
function onsquare24(e:MouseEvent):void {
sximata = square24;
}
square25.addEventListener(MouseEvent.CLICK, onsquare25);
function onsquare25(e:MouseEvent):void {
sximata = square25;
}
var myColorTransform:ColorTransform=transform.colorTransform;
red_btn.addEventListener(MouseEvent.CLICK, changeColour);
function changeColour(event:MouseEvent):void {
myColorTransform.color=0xBD8D46;
sximata.transform.colorTransform=myColorTransform;
}
resetButton.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
gotoAndPlay(1);
}
var square8Clicked:Boolean = false;
var square9Clicked:Boolean = false;
var square10Clicked:Boolean = false;
var square13Clicked:Boolean = false;
var square14Clicked:Boolean = false;
function checkButtons():void
{
if(square8Clicked && square9Clicked && square10Clicked && square13Clicked && square14Clicked)
{
text_mc.visible = true;
}
}
You could add a boolean variable to each of the other functions that turns to true if any of the other squares are clicked. For example:
var isClicked:Boolean = false;
square1.addEventListener(MouseEvent.CLICK, onsquare1);
function onsquare1(e:MouseEvent):void {
sximata = square1;
isClicked = true;
}
And then in your check buttons function, check to see if "isClicked" is still false:
function checkButtons():void
{
if(!isClicked && square8Clicked && square9Clicked && square10Clicked && square13Clicked && square14Clicked)
{
text_mc.visible = true;
}
}
My solution is below. It's based on counting the number of clicks received by each type of MovieClip the user can click on.The relevant parts of the code would be in the onClick() and checkClickCounts() methods.
First, you'll see that in the buildMCs() method I simply create a bunch of MovieClips and place them on the stage in a grid. I've made it so that the "specific" MCs that you mention are the first items on each row of the grid. To each of these "specific" MCs, I've added a property: isSpecial:Boolean and set it to true. Later, when a MC is clicked, the OnClick() method will check to see if the MC was special or not, and will increment the relevant click count property. Then, checkClickCounts() is called. If 5 good clicks and 0 bad clicks are counted up, then we let the user know. This is where you'd display your textfield. (In my case, I just draw a big red rectangle on the screen.
Another suggestion I demo here is to avoid repeating your mouse click code. If you look in the constructor, you'll see that I used this line:
this.addEventListener(MouseEvent.CLICK, onClick);
This tells the main stage sprite to listen to all mouse clicks, even the ones that happen to MovieClips inside of it. In the onClick() method I check to see if the item clicked - the target of the event - was a MovieClip. If it was, then I do the additional checking to see if the MC clicked was one that I wanted. By doing this, I was able to write the code for handling the mouse clicks once, and now if I need to change something, I only have to do it one time, rather than 25 times. Saves me time, but also makes the code less error-prone because I'm less likely to miss something if there's a change that needs to be made.
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Clicky extends Sprite
{
public function Clicky()
{
this.buildMCs();
this.addEventListener(MouseEvent.CLICK, onClick);
}
private function buildMCs ():void
{
var rows:int = 5;
var cols:int = 5;
var boxSize:int = 10;
for (var r:int = 0; r < rows; r++)
{
for (var c:int = 0; c < cols; c++)
{
var newMC:MovieClip = new MovieClip();
newMC.graphics.lineStyle(0, 0x00ff00);
// want to mark the "specific" movieclips
if (c == 0)
{
newMC.graphics.beginFill(0x0000ff);
// just something that makes this MC unique... ideally
// this would be not a MovieClip, but a class that defines
// actual properties worth checking for
newMC.isSpecial = true;
}
else
{
newMC.graphics.beginFill(0x00ff00);
}
newMC.graphics.drawRect(0, 0, boxSize, boxSize);
this.addChild(newMC);
newMC.x = (c * boxSize);
newMC.y = (r * boxSize);
}
}
}
private function onClick (e:MouseEvent):void
{
if (e.target is MovieClip)
{
var mc:MovieClip = e.target as MovieClip;
mc.alpha = .25;
// disable the clicking for the clicked item
mc.mouseEnabled = false;
if (mc.isSpecial)
{
_specialClicks++;
}
else
{
_badClicks++;
}
this.checkClickCounts();
}
}
private var _specialClicks:int = 0;
private var _badClicks:int = 0;
private function checkClickCounts ():void
{
if (_specialClicks == 5 && _badClicks == 0)
{
// do your thing - show the text_mc, play a sound, award a prize, etc.
this.graphics.beginFill(0xff0000);
this.graphics.drawRect(0, 0, 1000, 1000);
}
}
}
}

AS3: Issues with multiple save/load slots

I'm trying to take this very simple "game" and give it three save/load slots. Following a separate tutorial I can make it work with a single save slot but once I try adding more, it gives me the following error message.
1046:Type was not found or was not compile-time constant: save2.
1046:Type was not found or was not compile-time constant: save3.
I am new to actionscript 3 so I'm sure I'm being very newbish but I have tried to figure this out for quite some time now but just can't seem to. The whole thing is controlled by buttons already placed on the scene. I appreciate any help I can get.
The code:
import flash.net.SharedObject;
var saveDataObject:SharedObject;
var currentScore:Number = 0
init();
function init():void{
btnAdd.addEventListener(MouseEvent.CLICK, addScore);
btnSave1.addEventListener(MouseEvent.CLICK, save1);
btnSave1.addEventListener(MouseEvent.CLICK, saveData);
btnSave2.addEventListener(MouseEvent.CLICK, save2);
btnSave2.addEventListener(MouseEvent.CLICK, saveData);
btnSave3.addEventListener(MouseEvent.CLICK, save3);
btnSave3.addEventListener(MouseEvent.CLICK, saveData);
btnLoad1.addEventListener(MouseEvent.CLICK, save1);
btnLoad1.addEventListener(MouseEvent.CLICK, loadData);
btnLoad2.addEventListener(MouseEvent.CLICK, save2);
btnLoad2.addEventListener(MouseEvent.CLICK, loadData);
btnLoad3.addEventListener(MouseEvent.CLICK, save3);
btnLoad3.addEventListener(MouseEvent.CLICK, loadData);
}
function save1(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile1");
}
function save2(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile2");
}
function save3(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile3");
}
function addScore(e:MouseEvent):void{
currentScore += 1;
updateScoreText();
}
function saveData(e:MouseEvent):void{
saveDataObject.data.savedScore = currentScore;
trace("Data Saved!");
saveDataObject.flush();
trace(saveDataObject.size);
}
function loadData(e:MouseEvent):void{
currentScore = saveDataObject.data.savedScore;
updateScoreText();
trace("Data Loaded!");
}
function updateScoreText():void
{
txtScore.text = ("Score: " + currentScore);
trace("Score text updated");
}
I tried your code and it works like a charm...
Anyways, I've made a simpler version that doesn't use so many functions and Events.
Here is a pure AS3 version of it (just save it as Test.as3 and use as Document Class in Flash), but you can copy the content of the Test() method and paste in a action frame.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.SharedObject;
import flash.text.TextField;
public class Test extends Sprite
{
public function Test()
{
/***** START: Faking buttons and field *****/
var txtScore:TextField = new TextField();
addChild(txtScore);
var btnAdd:Sprite = new Sprite();
var btnSave1:Sprite = new Sprite();
var btnSave2:Sprite = new Sprite();
var btnSave3:Sprite = new Sprite();
var btnLoad1:Sprite = new Sprite();
var btnLoad2:Sprite = new Sprite();
var btnLoad3:Sprite = new Sprite();
var items:Array = [btnAdd, null, btnSave1, btnSave2, btnSave3, null, btnLoad1, btnLoad2, btnLoad3];
for (var i:int = 0; i < items.length; ++i)
{
var item:Sprite = items[i];
if (item)
{
item.graphics.beginFill(Math.random() * 0xFFFFFF);
item.graphics.drawRect(0, 0, 100, 25);
item.graphics.endFill();
item.x = 25;
item.y = i * 30 + 25;
addChild(item);
}
}
/***** END: Faking buttons and field *****/
var saveDataObject:SharedObject;
var currentScore:Number = 0
btnAdd.addEventListener(MouseEvent.CLICK, addScore);
btnSave1.addEventListener(MouseEvent.CLICK, save);
btnSave2.addEventListener(MouseEvent.CLICK, save);
btnSave3.addEventListener(MouseEvent.CLICK, save);
btnLoad1.addEventListener(MouseEvent.CLICK, load);
btnLoad2.addEventListener(MouseEvent.CLICK, load);
btnLoad3.addEventListener(MouseEvent.CLICK, load);
function getLocal(target:Object):String
{
if (target == btnSave1 || target == btnLoad1)
{
return "savefile1";
}
else if (target == btnSave3 || target == btnLoad2)
{
return "savefile2";
}
else if (target == btnSave2 || target == btnLoad3)
{
return "savefile3";
}
}
function save(e:MouseEvent):void
{
var local:String = getLocal(e.target);
saveDataObject = SharedObject.getLocal(local);
saveDataObject.data.savedScore = currentScore;
trace("Data Saved!");
saveDataObject.flush();
trace(saveDataObject.size);
}
function load(e:MouseEvent):void
{
var local:String = getLocal(e.target);
saveDataObject = SharedObject.getLocal(local);
currentScore = saveDataObject.data.savedScore;
updateScoreText();
trace("Data Loaded!");
}
function addScore(e:MouseEvent):void
{
currentScore += 1;
updateScoreText();
}
function updateScoreText():void
{
txtScore.text = ("Score: " + currentScore);
trace("Score text updated");
}
}
}
}

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);
}
}