Play animation through an image array - actionscript-3

I have about 200 photos that will run in a loop, there will be a fade-i and a fade-out between the images, is there any way to work with an array of images or something like that so I do not apply the same animation for all the images one by one?
Edit
I've tried this, but it don't respect my setInterval:
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.events.Event;
stage.addEventListener(Event.ENTER_FRAME, sendToBack);
function sendToBack(event:Event):void{
setInterval(function(){
setChildIndex(getChildAt(2), 0);
var my_mc = getChildAt(2);
getChildAt(1).visible = false;
getChildAt(0).visible = false;
TransitionManager.start(my_mc, {
type: Fade
});
}, 2000);
}

I got the desired result with this code:
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.events.Event;
var childsNum = numChildren,
frameInterval = 3000,
fadeDuration = 0.5;
function hideAll(){
var i;
for (i = 0; i < numChildren; i++) {
getChildAt(i).visible = false;
}
}
hideAll();
function animateFrame(){
var child = getChildAt(childsNum - 1),
myTM:TransitionManager = new TransitionManager(child);
myTM.startTransition({
type: Fade,
duration: fadeDuration
});
myTM.addEventListener("allTransitionsInDone", function(){
setChildIndex(getChildAt(childsNum - 1), 0);
getChildAt(1).visible = false;
});
}
setInterval(function(){
animateFrame();
}, frameInterval);

Related

Actionscript Loader Error #1009

I'm trying to make a basic loader that uses Splash.swf to then load myisogame.swf
I keep getting error #1009. It's a template we was given in class, originally the Splash.swf directed to Game.swf, but in the code below there is only one mention of where it directs to.
In the template Game.fla doesn't have any code in it.
Here is the Splash
package {
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
public class Splash extends MovieClip {
var myLoader:Loader;
var loadingAnim:LoadingAnimation;
var percentLoaded:TextField =new TextField();
public function Splash() {
// constructor code
myLoader = new Loader();
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
var blubox = new BluBox();
addChild(blubox);
blubox.x=200;
blubox.y=200;
loadingAnim = new LoadingAnimation();
addChild(loadingAnim);
loadingAnim.x=200;
loadingAnim.y=200;
loadingAnim.scaleX=0;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0xFF0000;
format.size = 20;
format.underline = false;
percentLoaded.defaultTextFormat = format;
addChild(percentLoaded);
percentLoaded.x=200;
percentLoaded.y=230;
myLoader.load(new URLRequest("myisogame.swf"));
}
function onProgress(evt:ProgressEvent):void {
var nPercent:Number = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
loadingAnim.scaleX = nPercent / 100;
percentLoaded.text = nPercent.toString() + "%";
trace("load%"+nPercent.toString());
}
function onComplete(evt:Event):void {
myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
loadingAnim.x=1000;
addChild(myLoader);
}
function onIOError(evt:IOErrorEvent):void {
trace("IOError loading SWF");
}
}
}
5
6
7
8
11
12
13
14
20
25
26
MyIsoGame code
package {
import flash.display.MovieClip;
import as3isolib.display.scene.IsoScene;
import as3isolib.display.IsoView;
import as3isolib.display.scene.IsoGrid;
import as3isolib.graphics.Stroke;
import as3isolib.display.primitive.IsoBox;
import as3isolib.display.IsoSprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import eDpLib.events.ProxyEvent;
import as3isolib.geom.Pt;
import as3isolib.geom.IsoMath;
import as3isolib.graphics.SolidColorFill;
// before class
public class MyIsoGame extends MovieClip {
public var scene:IsoScene;
public var view:IsoView ;
public var CELLSIZE:int = 30;//------This is very important to remember cell size. This will affect placement of objects
var zoomFactor:Number = 1;////Zoom Factor heerrrreee--------------------
var s1:IsoSprite = new IsoSprite();
var wr:IsoSprite = new IsoSprite();
var wre:IsoSprite = new IsoSprite();
var g1:IsoSprite = new IsoSprite();
var b1:IsoSprite = new IsoSprite();
var b2:IsoSprite = new IsoSprite();
var b3:IsoSprite = new IsoSprite();
public function MyIsoGame() {
// constructor code
trace("hello from constructor");
scene = new IsoScene();
view = new IsoView();
view.setSize((stage.stageWidth), stage.stageHeight);
view.clipContent = true;
view.showBorder = false;
view.addScene(scene);
view.addEventListener(MouseEvent.MOUSE_DOWN, onStartPan, false, 0, true);
view.addEventListener(MouseEvent.MOUSE_WHEEL, onZoom, false, 0, true);
addChild(view);
var g:IsoGrid = new IsoGrid();
g.addEventListener(MouseEvent.CLICK, gridClick);
g.cellSize=30;
g.setGridSize(6,6);
g.y=0;
g.x=0;
g.gridlines = new Stroke(2,0x666666);
g.showOrigin = false;
scene.addChild(g);
g.addEventListener(MouseEvent.CLICK, grid_mouseHandler);
var box:IsoBox = new IsoBox();
box.setSize(10, 10, 10);
box.moveTo(50,50,0);
scene.addChild(box);
wr.setSize(30, 30, 30);
wr.moveTo(60, 0, 0);
wr.sprites=[new Wrecked()];
scene.addChild(wr);
wre.setSize(30, 30, 30);
wre.moveTo(30, 0, 0);
wre.sprites=[new Wrecked()];
scene.addChild(wre);
g1.setSize(30, 30, 30);
g1.moveTo(150, 150, 0);
g1.sprites=[new Gold()];
scene.addChild(g1);
b1.setSize(30, 30, 30);
b1.moveTo(120, 120, 0);
b1.sprites=[new Blue()];
scene.addChild(b1);
b2.setSize(30, 30, 30);
b2.moveTo(120, 150, 0);
b2.sprites=[new Blue()];
scene.addChild(b2);
b3.setSize(30, 30, 30);
b3.moveTo(150, 120, 0);
b3.sprites=[new Blue()];
scene.addChild(b3);
scene.render();
stage.addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheel);///MOUSE WHEEL CONTROL
stage.addEventListener (KeyboardEvent.KEY_DOWN, keyboarddownlistener)
}
private function gridClick(event:ProxyEvent):void
{
var me:MouseEvent = MouseEvent(event.targetEvent);
var p:Pt = new Pt(me.localX, me.localY);
IsoMath.screenToIso(p);
y
}
private var panPt:Pt;
private function onStartPan(e:MouseEvent):void
{
panPt = new Pt(stage.mouseX, stage.mouseY);
view.removeEventListener(MouseEvent.MOUSE_DOWN, onStartPan);
view.addEventListener(MouseEvent.MOUSE_MOVE, onPan, false, 0, true);
view.addEventListener(MouseEvent.MOUSE_UP, onStopPan, false, 0, true);
}
private function onPan(e:MouseEvent):void
{
view.panBy(panPt.x - stage.mouseX, panPt.y - stage.mouseY);
panPt.x = stage.mouseX;
panPt.y = stage.mouseY;
}
private function boxClick(e:Event)
{
view.centerOnIso(e.target as IsoBox);
}
private function onStopPan(e:MouseEvent):void
{
view.removeEventListener(MouseEvent.MOUSE_MOVE, onPan);
view.removeEventListener(MouseEvent.MOUSE_UP, onStopPan);
view.addEventListener(MouseEvent.MOUSE_DOWN, onStartPan, false, 0, true);
}
private var zoomValue:Number = 1;
private function onZoom(e:MouseEvent):void
{
if(e.delta > 0)
zoomValue += 0.10;
if(e.delta < 0)
zoomValue -= 0.10;
view.currentZoom = zoomValue;
}
///----------------Grid Mouse Handler
public function grid_mouseHandler (evt:ProxyEvent):void
{
var mEvt:MouseEvent = MouseEvent(evt.targetEvent);
var pt:Pt = new Pt(mEvt.localX, mEvt.localY);
IsoMath.screenToIso(pt);
var roundedX:int = int(pt.x)/30;
var roundedY:int= int(pt.y)/30;
trace("transformed point = "+roundedX +","+roundedY);
///Code that allows things to be put down, located here.
var s:IsoSprite= new IsoSprite();
s.sprites=[new Base()];
s.setSize(30, 30, 30);//Varies via Cell size-
s.moveTo(roundedX*30, roundedY*30, 0);
scene.addChild(s);
scene.render();
}///------------------Grid Mouse Handler
public function mouseWheel (event:MouseEvent){
trace("The Delta value isss: " + event.delta);
//Get current view zoom
zoomFactor+=event.delta*0.04;
view.zoom(zoomFactor)
}
public function keyboarddownlistener(e:KeyboardEvent){
{//Screen-Movement Code
if (e.keyCode == 40)
{// The numbers represent the keyboard buttons
trace("Down Arrow")// I've left these trace commands so you can get a better idea of which one is what.-William 22/04/14
view.pan(0,5);
scene.render();
//Down arrow???
}
}
if (e.keyCode == 38)
{
trace("Up Arrow")
view.pan(0,-5);
scene.render();
//Up arrow???
}
if (e.keyCode == 37)
{
trace("Left Arrow")
view.pan(5,0);
scene.render();
//Left arrow???
}
if (e.keyCode == 39)
{
trace("Right Arrow")
view.pan(-5,0);
scene.render();
//Right arrow???
}
/////OBJECT MOVEMENT code- For moving the donut around
///-- I am going to be working on a version of this game where--
///- everything can be controlled via a Keyboard only -William 22/04/14
if (e.keyCode == 65)
{
trace("A Left <--")
//view.x+=15;//Alternate version
s1.moveBy(30,0,0)
scene.render();
//Left?
}
if (e.keyCode == 68)
{
trace("D Right -->")
//view.x-=15;//Alternate version
s1.moveBy(-30,0,0)
scene.render();
//Right?
}
if (e.keyCode == 83)
{
trace("S Down --v")
//view.x-=15;//Alternate version
s1.moveBy(0,30,0)
scene.render();
//Down?
}
if (e.keyCode == 87)
{
trace("W Up --^")
//view.x-=15;//Alternate version
s1.moveBy(0,-30,0)
scene.render();
//Up?
}
}
}
}
I tried the code you supplied (without the numbers at the bottom) and it worked just fine for me.
error #1009 is a runtime error when you use a variable that hasn't been defined yet. I would suggest debugging from Flash Pro (ctrl + shift +enter) and let it show you where it breaks.
In your FLA, you said there is no code, but in the properties panel you do see the class field with "Splash" in it right? that is what links the class to the FLA.
It is possible that the runtime error you are seeing is not in the Splash class but in either BluBox or LoadingAnimation. Do those have class files? If not, take a look in the Library and you'll have two movie clips that are exported for actionScript with those names. Check in them to see if there might be some framescript.

error in my actionscript for a sound slider I'm working on

Hello I' trying to make a sound slider in flash, while working on the code for it; I keep on getting this error Scene 1, 'S Action', Frame 352, line 8 1152: A conflict exists with inherited definition flash.display:MoveClip.isPlaying in namespace public. I'm wondering what I done to cause this error and how can I fix it?
Please get back to me if you can.
stop();
import flash.media.SoundTransform;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.SoundChannel;
var isPlaying:Boolean = true;
var lastPosition:Number = 0;
var mySound:takeachance = new takeachance ;
var myChannel:SoundChannel = new SoundChannel();
var myTransform:SoundTransform = new SoundTransform();
myTransform.volume = .5;
slider02_mc.groove02_mc.scaleX = .5;
myChannel = mySound.play(85,5,myTransform);
myChannel.addEventListener(Event.SOUND_COMPLETE,
soundCompleteHandler);
function soundCompleteHandler(e:Event):void
{
lastPosition=0;
myChannel.stop();
msg_mc.text = "Music stoped playing"
isPlaying = false;
}
play_btn.addEventListener(MouseEvent.CLICK,startSound);
pause_btn.addEventListener(MouseEvent.CLICK,stopSound);
function startSound(myEvent:MouseEvent):void
{
if(!isPlaying)
{
myChannel = mySound.play(lastPosition,5,myTransform);
isPlaying = true;
msg_mc.text="";
myChannel.addEventListener(Event.SOUND_COMPLETE,
soundCompleteHandler);
}
}
function stopSound(myEvent:MouseEvent):void
{
lastPostion = myChannel.position;
myChannel.stop();
isPlaying = false;
}
slider02_mc.mc.addEventListener(MouseEvent.MOUSE_DOWN, myfunction);
function myfunction(event:MouseEvent):void
{
slider02_mc.mc.startDrag(false,new Rectangle(0,0,200,0))
addEventListener(Event.ENTER_FRAME,xpos);
function xpos(event:Event)
{
var myvolume=(slider02_mc.mc.x)/200;
slider02_mc.groove02_mc.scaleX = myvolume;
volume_mc.text = "Volume is"+int(myvolume*100)+"%";
myTransform.volume = myvolume;
myChannel.soundTransform = myTransform;
}
}
stage.addEventListener(MouseEvent.MOUSE_UP,myfunction1);
function myfunction1(event:MouseEvent):void
{
slider02_mc.mc.stopDrag();
addEventListener(Event.ENTER_FRAME,msg);
function msg(event:Event)
{
volume_mc.text="";
}
}
Movieclips already have a property called isPlaying. You should change your variable to something else like soundIsPlaying.

How to add 2 volume sliders to my mp3 players in as3

Easy people. Im currently trying to add 2 volume sliders to my existing project. I have two turntables, deck1 & deck2 but i need to be able to control the volume of each deck individually.
Im relatively new to actionscript 3 so im struggling abit. Can anyone help me please, here is my code..
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.text.TextField;
//variables
var mySound:Sound;
var myChannel:SoundChannel;
var nowPlaying:Boolean = false;
var nowPaused:Boolean = false;
var p:uint = 0;
var songfile:String;
var songtitle:String;
deck1_btn.addEventListener(MouseEvent.CLICK, deck1_data);
deck2_btn.addEventListener(MouseEvent.CLICK, deck2_data);
//song3_btn.addEventListener(MouseEvent.CLICK, song3_data);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
stop2_btn.addEventListener(MouseEvent.CLICK, stopSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
function deck1_data(myEvent:MouseEvent):void {
songfile = "audio/desire.mp3";
songtitle = "Skeptical - Desire";
playSound(null);
}
function deck2_data(myEvent:MouseEvent):void {
songfile = "audio/tundra.mp3";
songtitle = "Skeptical - Tundra";
playSound(null);
}
//function song3_data(myEvent:MouseEvent):void {
songfile = "audio/always_be_mine.mp3";
songtitle = "Skeptical - Always Be Mine";
playSound(null);
function stopSound(myEvent:MouseEvent):void {
if (nowPlaying) {
myChannel.stop();
p = 0;
nowPlaying = false;
nowPaused = false;
}
}
function playSound(myEvent:Event):void {
mySound = new Sound;
mySound.load(new URLRequest(songfile));
title_txt.text = songtitle;
if (isPlaying) {
myChannel.stop();
myChannel = mySound.play(0);
} else {
myChannel = mySound.play(0);
nowPlaying = true;
}
}
function pauseSound(myEvent:MouseEvent):void {
if (isPlaying) {
p = Math.floor(myChannel.position);
myChannel.stop();
nowPlaying = false;
nowPaused = true;
} else if (nowPaused) {
myChannel = mySound.play(p);
nowPlaying = true;
nowPaused = false;
}
}
title_txt.text = "";
For controlling volume you will need SoundTransform. Create sliders, and store volumes as numbers. And when you start deck sound, apply soundTransform with deck volume value:
myChannel = mySound.play(0, 0, new SoundTransform(myDeck1Volume));
//or
//myChannel = mySound.play(0, 0, new SoundTransform(myDeck2Volume));
As for sliders. You could use Slider component

AS3 BitmapData Collision

I am having a problem with BitmapData Collision detection, my class is not detecting any collision at all
BitmapCollision.as
package com.ipromoweb.demoapp
{
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
/**
* ...
* #author Me
*/
public class BitmapCollision {
private var collides:Boolean = false;
public function check(obj1:DisplayObjectContainer, obj2:DisplayObjectContainer):Boolean {
// obj1
var obj1Rect:Rectangle = obj1.getBounds(obj1);
var obj1Offset:Matrix = obj1.transform.matrix;
obj1Offset.tx = obj1.x - obj1Rect.x;
obj1Offset.ty = obj1.y - obj1Rect.y;
var obj1ClipBmpData:BitmapData = new BitmapData(obj1Rect.width, obj1Rect.height, true, 0);
obj1ClipBmpData.draw(obj1, obj1Offset);
// obj2
var obj2Rect:Rectangle = obj2.getBounds(obj2);
var obj2Offset:Matrix = obj2.transform.matrix;
obj2Offset.tx = obj2.x - obj2Rect.x;
obj2Offset.ty = obj2.y - obj2Rect.y;
var obj2ClipBmpData:BitmapData = new BitmapData(obj2Rect.width, obj2Rect.height, true, 0);
obj2ClipBmpData.draw(obj2, obj2Offset);
var rLoc:Point = new Point(obj2Rect.x, obj2Rect.y);
var bLoc:Point = new Point(obj1Rect.x, obj1Rect.y);
if (obj1ClipBmpData.hitTest(bLoc, 255, obj2ClipBmpData, rLoc, 255)) {
trace("hit");
collides = true;
}else {
collides = false;
}
obj1ClipBmpData.dispose();
obj2ClipBmpData.dispose();
return collides;
}
}
}
And i am calling checkCollision function on a ENTER_FRAME event, the hitTestObject is firing well but the bitmapData Collision is not firing at all.
private function checkCollision():void {
for (var i:int = 0; i < parent.numChildren; i++) {
if (Object(parent.getChildAt(i)).constructor == '[class CollisionTest]') {
if (this.hitTestObject(parent.getChildAt(i))) {
trace('colliding');
if ( _collision.check(Sprite(this),Sprite(parent.getChildAt(i))) ) {
trace('>>>>>>>>>>> perfect hit');
}
}
}
}
}
Any help on this would be greatly appreciated.
I discarded the previous method of BitmapData Collision that i was trying to implement and downloaded a free library called CDK:
https://code.google.com/p/collisiondetectionkit/
it solved all my problems.

actionscript 3 synchronous loader

I have typical situation where big loop is loading lots of images and its done asynchronous which make browser to frees during loading and I want to make it synchronous but having big trouble doing it. I found this class synchronous loader and it work great but you cant add mouse event listener to loader. Here is sample code:
for (var i = 0; i < items.length; i++) {
item = items[i];
if(item.layer>1){
ld:Loader = new Loader();
ld.load(new URLRequest(item.url));
ld.rotation = item.rotation;
ld.x = item.x ;
ld.y = item.y;
ld.addEventListener(Event.COMPLETE, loadComplete);
ld.scaleX = item.scaleX;
ld.scaleY = item.scaleY;
ld.addEventListener(MouseEvent.MOUSE_DOWN, select);
layers_arr[item.layer].addChild(ld);
}
}
Any idea how this can be done?
like arieljake says, here is a little example on how to use it:
package
{
import com.greensock.TweenLite;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.ImageLoader;
import com.greensock.loading.LoaderMax;
import com.greensock.loading.MP3Loader;
import com.greensock.loading.SWFLoader;
import com.greensock.loading.XMLLoader;
import com.greensock.loading.display.ContentDisplay;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class Main extends Sprite
{
public var itemUrl:String;
public var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
queue.maxConnections = 1; //Checks how much items that can be loaded at the same time
queue.append( new ImageLoader("http://www.myurl.com/myimage.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0,scaleMode:"proportionalInside"}) );
queue.append( new ImageLoader("http://www.myotherurl.com/awesomeimage.jpg", {name:"photo2", estimatedBytes:2400, container:this, alpha:0, scaleMode:"proportionalInside"}) );
queue.addEventListener(LoaderEvent.CHILD_COMPLETE, childCompleteHandler); //checks when a child has completed to load
queue.addEventListener(LoaderEvent.CHILD_PROGRESS, childProgressHandler); //checks the child progress
//prioritize the loader named "photo1"
LoaderMax.prioritize("photo1"); //same as LoaderMax.getLoader("photo1").prioritize();
//start loading
queue.load();
}
protected function childProgressHandler(event:LoaderEvent):void
{
var procent:Number = Math.floor(event.target.progress*100);
var targetName:String = event.target.name;
trace(procent+'% loaded of item: '+targetName);
}
protected function childCompleteHandler(event:LoaderEvent):void
{
var targetName:String = event.target.name;
trace(targetName+' is loaded!');
}
private function completeHandler(event:LoaderEvent):void {
var objects:Array = event.currentTarget.content;
for(var i:uint=0; i < objects.length; i++)
{
var image:ContentDisplay = LoaderMax.getContent(objects[i].name);
TweenLite.to(image, 1, {alpha:1, y:100});
}
trace(event.target + " is complete!");
}
private function errorHandler(event:LoaderEvent):void {
trace("error occured with " + event.target + ": " + event.text);
}
}
}
Check this out: http://www.greensock.com/loadermax/
Let's you specify max number of simultaneous loaders