I have a set of externally loaded images lined up horizontally. The problem is I’m trying to make it where as the images are dragged past the boundaries of the stage, the images will pop out the other side.
Kind of like this:
[. :!:;P:king:…]
[… :!:;P:king:.]
[…:!:;P:king:]
[ …:!:
;P]
[;P:king: …:!:]
lol
Here is a link to my files: Home - Uploading.com
And here is the code:
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.filters.GlowFilter;
var gf:GlowFilter = new GlowFilter(uint(0xFFFF00),.9,9,9,4);//selection glow
var dragging:Boolean = false;
var target:Sprite;
var initPos:Number;
var distMoved:Number;
var mouseDifferenceX:Number;
var padding:int = 10;
var images:Array = new Array();
var bg1:Shape = new Shape();
bg1.graphics.beginFill(0x0000FF,1);
bg1.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
bg1.graphics.endFill();
addChild(bg1);
var bg2:Shape = new Shape();
var gradientBoxMatrix:Matrix = new Matrix();
gradientBoxMatrix.createGradientBox(stage.stageWidth, 150);
bg2.graphics.lineStyle(1);
bg2.graphics.beginGradientFill(GradientType.LINEAR, [0x000000,
0x666666, 0x000000], [1, 1, 1], [0, 128, 255], gradientBoxMatrix);
bg2.graphics.drawRect(0,0,stage.stageWidth,150);
var bgSprite:Sprite = new Sprite()
bgSprite.addChild(bg2);
addChild(bgSprite);
var containerTop:Number = bgSprite.y + (bgSprite.height/4);
var headSpot:questionHead = new questionHead();
headSpot.x = (stage.stageWidth/2 - headSpot.width/2);
headSpot.y = (bgSprite.y + bgSprite.height) + 15;
headSpot.width = headSpot.width/2;
headSpot.height = headSpot.height/2;
addChild(headSpot);
//load external images
var imageLoader:ImageLoader = new ImageLoader("images/image", 4, ".png");
imageLoader.addEventListener("ImagesLoaded", imagesLoaded);
function imagesLoaded(e:Event):void
{
images = imageLoader.images;
var firstLoop:Boolean = true;
for(var i:int = 0; i < images.length; i++)
{
if(!firstLoop)
images*.x = images[(i-1)].x + (images*.width) + padding;
firstLoop = false;
images*.y = containerTop;
images*.buttonMode = true;
images*.addEventListener(MouseEvent.MOUSE_OVER, mOver_image);
images*.addEventListener(MouseEvent.MOUSE_OUT, mOut_image);
images*.addEventListener(MouseEvent.MOUSE_DOWN, mDown_image);
images*.addEventListener(MouseEvent.MOUSE_UP, mUp_image);
images*.addEventListener(MouseEvent.CLICK, mClick_image);
addChild(images*);
}
addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp_image);
}
function mDown_image(e:MouseEvent):void
{
target = Sprite(e.currentTarget);
Mouse.cursor = MouseCursor.HAND;
initPos = target.x;
distMoved = 0;
mouseDifferenceX = stage.mouseX - target.x;
dragging = true;
}
function mUp_image(e:MouseEvent):void
{
Mouse.cursor = MouseCursor.AUTO;
dragging = false;
}
function mOver_image(e:MouseEvent):void
{
e.currentTarget.filters = [gf];
}
function mOut_image(e:MouseEvent):void
{
e.currentTarget.filters = [];
}
function mClick_image(e:MouseEvent):void
{
//------------- clone selected sprite ---------------------------
//remove filters so filters are not cloned
e.currentTarget.filters = [];
e.currentTarget.scaleX = 1;
e.currentTarget.scaleY = 1;
//copy bitmap data from selected sprite
var bd:BitmapData = new BitmapData(e.currentTarget.width, e.currentTarget.height, true);
bd.draw(Sprite(e.currentTarget));
//replace white background with transparent pixels
bd.threshold(bd, new Rectangle(0,0,e.currentTarget.width,e.currentTarget.height), new Point(0, 0), "==", 0xFFFFFF, 0x000000, 0xFFFFFF, true);
var clone:Sprite = new Sprite();
clone.graphics.beginBitmapFill(bd, null, false, true);
clone.graphics.drawRect(0, 0, bd.width, bd.height);
clone.graphics.endFill();
clone.x = headSpot.x;
clone.y = headSpot.y;
clone.width = headSpot.width;
clone.height = headSpot.height;
addChild(clone);
}
function loop(e:Event):void
{
for(var j:int = 0; j < images.length; j++)
{
//does not work ------------------------
if(images[j].x > stage.stageWidth)
images[j].x = -images[j].width;
if(images[j].x < -images[j].width)
images[j].x = stage.stageWidth;
//---------------------------------------
}
if(dragging)
{
if(stage.mouseX > 0 && stage.mouseX < stage.stageWidth)
{
var beforeMove:Number = target.x;
//target will follow mouse on x-axis only
target.x = stage.mouseX - mouseDifferenceX;
distMoved = target.x - beforeMove;
//have all other images move the same dist and direction as target
for(var i:int = 0; i < images.length; i++)
{
if(images* != target)
{
images*.x += distMoved;
}
}
}
}
}
//does not work ------------------------
if(images[j].x > stage.stageWidth)
images[j].x = -images[j].width;
if(images[j].x < -images[j].width)
images[j].x = stage.stageWidth;
//---------------------------------------
This is what I’m doing atm, but no luck.
There are no errors just a weird bug I’ve been trying to figure out.
Thanks in advance to anyone willing to take a look at it. :hugegrin: