gallery last image fix - actionscript-3

I almost have this thing the way I want it except when I'm clicking forward or backwards through my images, once it hits the array length, the next images doesn't show. It is only an issue when I add the checkImage() method. Anyone see what I have wrong?
fix**
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import com.greensock.TweenMax;
import com.greensock.layout.*;
import com.greensock.loading.LoaderMax;
import com.greensock.loading.XMLLoader;
import com.greensock.loading.ImageLoader;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.display.ContentDisplay;
import com.greensock.loading.data.XMLLoaderVars;
public class Main extends MovieClip
{
public var imgHolder:MovieClip;
private var ls:LiquidStage;
private var la:LiquidArea;
private var xml:XMLLoader;
private var index:int = 0;
private var images:Array;
public function Main()
{
arrowRight.alpha = arrowLeft.alpha = .5;
xmlLoader();
}
private function xmlLoader():void
{
LoaderMax.activate([ImageLoader]);
xml = new XMLLoader("assets/data.xml", new XMLLoaderVars()
.name("loader")
.estimatedBytes(600000)
.onComplete(xmlLoaded)
.onProgress(loadProgress));
xml.load();
}
private function xmlLoaded(e:LoaderEvent):void
{
trace("Loaded");
arrowListeners();
showImage(index);
}
private function loadProgress(event:LoaderEvent):void
{
progressMC.progressBar.scaleX = event.target.progress;
}
private function showImage(index:int):void
{
ls = new LiquidStage(this.stage, 1024, 768, 1024, 768);
la = new LiquidArea(imgHolder, 0, 0, 1024, 768);
images = LoaderMax.getContent("loader");
imgHolder.addChild(images[index]);
TweenMax.from(images[index], 1, {alpha:0});
la.attach(images[index], {scaleMode:ScaleMode.PROPORTIONAL_OUTSIDE, crop:true});
}
// BUTTON FUNCTIONS
private function arrowListeners():void
{
arrowRight.addEventListener(MouseEvent.ROLL_OVER, rollOverArrowHandler, false, 0, true);
arrowRight.addEventListener(MouseEvent.ROLL_OUT, rollOutArrowHandler, false, 0, true);
arrowRight.addEventListener( MouseEvent.CLICK, showNext );
arrowLeft.addEventListener(MouseEvent.ROLL_OVER, rollOverArrowHandler, false, 0, true);
arrowLeft.addEventListener(MouseEvent.ROLL_OUT, rollOutArrowHandler, false, 0, true);
arrowLeft.addEventListener( MouseEvent.CLICK, showPrev );
}
private function rollOverArrowHandler(e:MouseEvent):void
{
TweenMax.to(e.currentTarget, 0.5, {alpha:1});
}
private function rollOutArrowHandler(e:MouseEvent):void
{
TweenMax.to(e.currentTarget, 0.5, {alpha:.5});
}
// CLICK LISTENERS
private function showNext( e:MouseEvent ):void
{
index++
if (index > images.length - 1)
{
index = 0;
}
showImage(index);
}
private function showPrev( e:MouseEvent ):void
{
index--
if (index < 0)
{
index = images.length - 1;
}
showImage(index);
}
}
}

Several things come to mind:
1) index is a global variable and a function param - conflicting?
2) in the checkimage function you pass in 'index' but refer to 0 in getChildAt and RemoveChildAt
3) you don't call showimage in checkimage if the numchildren > 0 is true
I'd want to trace into all of this to sort it out.

Related

Not Displaying Nape Body

I had these two in the same Main class. Now there are Main.as and MainChar.as
When compiling doesn't show any errors but it isn't displaying the object either :c
It is the first time I am splitting code into different classes. I just figured what should be in the hero creation class and what should stay in Main.
package
{
import flash.display.Sprite;
import flash.events.Event;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyList;
import nape.space.Space;
import MainChar;
public class Main extends Sprite
{
public var gravity:Number = 600;
public var space:Space = new Space(new Vec2(0, gravity));
public var hero:MainChar = new MainChar();
public function Main():void
{
hero.createMainCharacter(stage.stageWidth/2, stage.stageHeight/2, 50, 50);
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void
{
space.step(1 / stage.frameRate, 10, 10);
var bodies:BodyList = space.bodies;
for (var i:int = 0; i < bodies.length; i++)
{
var body:Body=bodies.at(i);
if (body.userData.sprite != null)
{
body.userData.sprite.x = body.position.x;
body.userData.sprite.y = body.position.y;
body.userData.sprite.rotation=(body.rotation*180/Math.PI)%360;
}
}
}
}
}
Hero creator class:
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyType;
import nape.shape.Polygon;
import nape.space.Space;
public class MainChar extends Sprite
{
public var space:Space = new Space(new Vec2(0, 600));
public function MainChar():void
{
}
public function createMainCharacter(x:Number, y:Number, width:Number, height:Number):void
{
var mainChar:Body = new Body(BodyType.DYNAMIC);
var mainCharShape:Polygon = new Polygon(Polygon.box(width, height));
mainChar.shapes.add(mainCharShape);
mainChar.position.setxy(x, y);
mainChar.space = space;
var mainCharSprite:Sprite = new Sprite();
mainCharSprite.graphics.beginFill(0x000000);
mainCharSprite.graphics.drawRect( -width/2, -height/2, width, height);
mainCharSprite.graphics.endFill;
addChild(mainCharSprite);
mainChar.userData.sprite = mainCharSprite;
addChild(mainChar.userData.sprite);
}
}
}
You need to add your hero as a child of your Main sprite EG:
public function Main():void
{
hero.createMainCharacter(stage.stageWidth/2, stage.stageHeight/2, 50, 50);
addEventListener(Event.ENTER_FRAME, update);
addChild(hero);
}

addChild/init issue

hey im doing the "Flash Bubbles: Paricle Systems with TimelineMax" from youtube. i got this code:
package
{
import com.greensock.*;
import com.greensock.easing.*;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class main extends Sprite
{
private var BG:bg= new bg;
private var start_btn:start_button= new start_button;
private var tl:TimelineMax= new TimelineMax();
private var bubbleMax:Number = 50;
public function main()
{
stage.addChild(BG);
stage.addChild(start_btn);
BG.y= (stage.stageWidth)/2;
BG.x = (stage.stageWidth)/2;
start_btn.x = (stage.stageWidth)/2;
start_btn.y = (stage.stageHeight)/2;
start_btn.addEventListener(MouseEvent.CLICK,StartGame);
}
private function StartGame(e:Event)
{
stage.removeChild(BG);
stage.removeChild(start_btn);
createBubble();
}
private function createBubble()
{
var Bubble:bubble= new bubble();
Bubble.y=200;
Bubble.x= randomRange(50,1100);
Bubble.alpha= .5;
addChild(Bubble);
}
private function randomRange(min:Number,Max:Number):Number
{
return min + (Math.random() * (Max - min));
}
private function init()
{
for (var count:Number = 0; count<bubbleMax; count++)
{
createBubble();
}
}
init();
}
}
bg, start_button, and bubble are all movie clips made in flash (and been given a as3 class)
I'm aspect to 50 bubbles, but can only see one.. thanks for help!
Change the StartGame function to this:
private function StartGame(e:Event)
{
stage.removeChild(BG);
stage.removeChild(start_btn);
// Your init function is where you're creating all the bubbles. That's what the "for" loop is doing.
init();
}
Also, don't call init(); towards the bottom like you are doing. Your code should look more like this:
package
{
import com.greensock.*;
import com.greensock.easing.*;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class main extends Sprite
{
private var BG:bg= new bg();
private var start_btn:start_button= new start_button();
private var tl:TimelineMax= new TimelineMax();
private var bubbleMax:Number = 50;
public function main()
{
stage.addChild(BG);
stage.addChild(start_btn);
BG.y= (stage.stageWidth)/2;
BG.x = (stage.stageWidth)/2;
start_btn.x = (stage.stageWidth)/2;
start_btn.y = (stage.stageHeight)/2;
start_btn.addEventListener(MouseEvent.CLICK,StartGame);
}
private function StartGame(e:Event)
{
stage.removeChild(BG);
stage.removeChild(start_btn);
init();
}
private function createBubble()
{
var Bubble:bubble= new bubble();
Bubble.y=200;
Bubble.x= randomRange(50,1100);
Bubble.alpha= .5;
addChild(Bubble);
}
private function randomRange(min:Number,Max:Number):Number
{
return min + (Math.random() * (Max - min));
}
private function init()
{
for (var count:Number = 0; count<bubbleMax; count++)
{
createBubble();
}
}
}
}

Audio seek bar control on flash

I have a scenario , I am developing a audio player and want to write code for audio track seek bar.
Any help is appreciated.
Assuming you have a Sound object and a SoundChannel object to play the Sound object, you can do something like this:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var _trackBar:Sprite;
private var _sound:Sound;
private var _soundChannel:SoundChannel;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_trackBar = new Sprite();
_trackBar.graphics.beginFill(0);
_trackBar.graphics.drawRect(-10, 0, 20, 40);
_trackBar.graphics.endFill();
addChild(_trackBar);
graphics.lineStyle(2);
graphics.moveTo(_trackBar.x - 10, _trackBar.y);
graphics.lineTo(_trackBar.x - 10, _trackBar.y +_trackBar.height);
graphics.moveTo(_trackBar.x + 200, _trackBar.y);
graphics.lineTo(_trackBar.x + 200, _trackBar.y + _trackBar.height);
_sound = new Sound();
_sound.addEventListener(Event.COMPLETE, onLoaded);
_sound.load(new URLRequest("[mp3 url goes here].mp3"));
}
private function onLoaded(e:Event):void
{
_soundChannel = _sound.play();
stage.addEventListener(Event.ENTER_FRAME, onTick);
_trackBar.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
_trackBar.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onTick(e:Event):void
{
if (!_soundChannel)
{
return;
}
_trackBar.x = 200 * (_soundChannel.position / _sound.length);
}
private function onMouseUp(e:MouseEvent):void
{
_trackBar.stopDrag();
_soundChannel = _sound.play(_sound.length * (_trackBar.x / 200));
}
private function onMouseDown(e:MouseEvent):void
{
_soundChannel.stop();
_soundChannel = null;
_trackBar.startDrag(false, new Rectangle(0, 0, 200, 0));
}
}
}
Please note: this is very rough and can definitely be optimized. It's just something thrown together to show you how you would accomplish such a task.

LoaderMax image resize

I am trying to load xml images using LoaderMax and resize them using LiquidArea. When I try to load in a different image with resize using next or thumbnails, I get a weird error I can't understand. I was not able to get help so I've come here. The error message and code can be seen below.
RangeError: Error #2006: The supplied index is out of bounds.
at flash.display::DisplayObjectContainer/addChildAt()
at com.greensock.layout::AutoFitArea/set preview()
at com.greensock.layout.core::LiquidData/refreshLevel()
at com.greensock.layout::LiquidStage/refreshLevels()
at com.greensock.layout.core::LiquidData$/addCacheData()
at com.greensock.layout::LiquidArea/pinCorners()
at com.greensock.layout::LiquidArea/autoPinCorners()
at com.greensock.layout::LiquidArea()
at Main/loadImage()
at Function/<anonymous>()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at com.greensock.core::TweenCore/complete()
at com.greensock::TweenMax/complete()
at com.greensock::TweenMax/renderTime()
at com.greensock.core::SimpleTimeline/renderTime()
at com.greensock::TweenLite$/updateAll()
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import com.greensock.TweenMax;
import com.greensock.layout.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.XMLLoader;
import com.greensock.loading.LoaderMax;
import com.greensock.loading.ImageLoader;
import com.greensock.loading.data.ImageLoaderVars;
import com.greensock.loading.display.ContentDisplay;
public class Main extends MovieClip
{
private static const THUMB_WIDTH:Number = 100;
private static const THUMB_HEIGHT:Number = 64;
private static const IMAGE_WIDTH:Number = 550;
private static const IMAGE_HEIGHT:Number = 355;
private var ls:LiquidStage;
private var la:LiquidArea;
private var xImgList:XMLList;
public var arrowRight:MovieClip;
public var arrowLeft:MovieClip;
private var currentImage:String;
private var image:ImageLoader;
private var index:Number = 0;
public function Main()
{
// load in xml
var xPhotography:XMLLoader = new XMLLoader("assets/data.xml");
xPhotography.addEventListener( LoaderEvent.COMPLETE, xmlLoaded );
xPhotography.load();
arrowRight.alpha = arrowLeft.alpha = 0;
arrowRight.addEventListener(MouseEvent.ROLL_OVER, rollOverArrowHandler, false, 0, true);
arrowRight.addEventListener(MouseEvent.ROLL_OUT, rollOutArrowHandler, false, 0, true);
arrowRight.addEventListener( MouseEvent.CLICK, onArrowClick );
arrowLeft.addEventListener(MouseEvent.ROLL_OVER, rollOverArrowHandler, false, 0, true);
arrowLeft.addEventListener(MouseEvent.ROLL_OUT, rollOutArrowHandler, false, 0, true);
arrowLeft.addEventListener( MouseEvent.CLICK, onArrowClick );
}
private function xmlLoaded( e:LoaderEvent ):void
{
var xData:XML = e.target.content;// get copy of xml
xImgList = new XMLList(xData.image);// grabbing xml image nodes
loadImage( 0 );
}
// load in the image
private function loadImage( index: Number ):void
{
ls = new LiquidStage(this.stage,550,419);
la = new LiquidArea(imgContainer, 0, 0, 550, 419);
var file:String = xImgList[index]. # url;
var image:ImageLoader = new ImageLoader("assets/images/" + file, new ImageLoaderVars()
.container( imgContainer )
.x(0)
.y(0)
.alpha( 0 )
.width( IMAGE_WIDTH )
.height( IMAGE_HEIGHT )
.onComplete(completeHandler)
.scaleMode( "proportionalOutside" )
);
image.load();
la.attach(image.content, {scaleMode:ScaleMode.PROPORTIONAL_OUTSIDE, crop:true});
}
private function completeHandler(event:LoaderEvent):void
{
TweenMax.to(event.target.content, 1.5, {alpha:1});
}
private function rollOverArrowHandler(e:MouseEvent):void
{
TweenMax.to(e.currentTarget, 0.5, {alpha:1});
}
private function rollOutArrowHandler(e:MouseEvent):void
{
TweenMax.to(e.currentTarget, 0.5, {alpha:0});
}
private function onArrowClick( e:MouseEvent ):void
{
switch (e.target)
{
case arrowRight :
index++;
break;
case arrowLeft :
index--;
break;
}
if (index == xImgList.length())
{
index = 0;
} else if (index < 0)
{
index = xImgList.length() - 1;
}
checkOldImage( index );// needed to help loading times
}
private function checkOldImage( index:Number ):void
{
var oldClip:DisplayObject = imgContainer.getChildAt( 0 );
var nextIndex = index;
TweenMax.to( oldClip, .5, { autoAlpha: 0, onComplete: completeFadeHandler, onCompleteParams:[oldClip] } );
function completeFadeHandler(child:DisplayObject):void {
if (child.parent) {
child.parent.removeChild(child);
}
loadImage(nextIndex);
}
}
}
}

AS3 ActionScript 3 - Instantiating Objects With A Timer?

I'm making a vertical (constantly) scrolling shooter & am trying to instantiate objects based on a timer. For example: At 30 seconds, place a building # x, y.
My problem is that the "building" is instantiated when the game starts and then again at the 30 second mark - instead of only # the 30 second mark.
If anyone could steer me in the correct direction, it would be greatly appreciated.
package com.gamecherry.gunslinger
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class ObjectPlacer extends MovieClip
{
private var Build01Timer:Timer;
private var canPlace:Boolean = true;
private var stageRef:Stage;
private var startX:Number;
private var startY:Number;
private var time:int = 5000;
public function ObjectPlacer(stageRef:Stage) : void
{
this.stageRef = stageRef;
var Build01Timer = new Timer(time, 1);
Build01Timer.addEventListener(TimerEvent.TIMER, placeTimerHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
Build01Timer.start();
}
private function loop(e:Event): void
{
if (canPlace)
{
var BuildingsLeft01:BuildingsLeft = new BuildingsLeft(stage, 720, -540);
BuildingsLeft01.scaleX = -1;
stageRef.addChildAt((BuildingsLeft01), 2);
canPlace = false;
}
}
private function placeTimerHandler(e: TimerEvent) : void
{
canPlace = true;
}
private function removeSelf() : void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
}
}
Where am I going wrong?
Thank you for looking.
Here's the first of your class:
public class ObjectPlacer extends MovieClip
{
private var Build01Timer:Timer;
**private var canPlace:Boolean = true;**
You're setting it to TRUE at the start, set it to false, and that would solve your problem :)