How to call one of the function from this class? - actionscript-3

I wanna call the function "nextMenu" & "prevMenu" from this class from Main.as
But I get the error 1136: Incorrect number of arguments. Expected 1.
Can help me see what I've left out on the codes?
CategoryScroller.as
package com.theflashfactor.carouselStackGallery.categoryMenu
{
import com.greensock.TweenMax;
import com.greensock.easing.Quint;
import com.theflashfactor.utils.Ref;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
/**
* Scroller to switch view for category item if there are more than visibleItem available
* #author Rimmon Trieu
*/
public class CategoryScroller extends Sprite
{
private var trackLength:int = 400;
private var scrubber:Sprite;
private var track:Shape;
private var categoryMenu:CategoryMenu;
private var trans:Sprite;
private var center:Number;
public function CategoryScroller(categoryMenu:CategoryMenu)
{
this.categoryMenu = categoryMenu;
buttonMode = true;
initialize();
}
/**
* Draw elemnt scrubber and track
*/
private function initialize():void
{
trans = new Sprite();
trans.graphics.beginFill(0, 0);
trans.graphics.drawRect(0, 0, trackLength, 13);
trans.graphics.endFill();
trans.addEventListener(MouseEvent.MOUSE_DOWN, transMouseDown, false, 0, true);
addChild(trans);
// Draw track
var color:uint = uint(Ref.getInstance().getRef("categoryCircleColor"));
track = new Shape();
track.graphics.lineStyle(1, color);
track.graphics.lineTo(trackLength, 0);
track.y = 6;
addChild(track);
// Draw scrubber
scrubber = new Sprite();
scrubber.graphics.beginFill(color);
scrubber.graphics.drawRect(0, 0, 80, 13);
scrubber.graphics.endFill();
center = (trackLength - scrubber.width) >> 1;
scrubber.x = center;
addChild(scrubber);
// Add dragging functionality
scrubber.addEventListener(MouseEvent.MOUSE_DOWN, scrubberMouseDown,false,0,true);
}
private function transMouseDown(event:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler,false,0,true);
TweenMax.killTweensOf(scrubber);
categoryMenu.preTransition();
if (mouseX > width - scrubber.width) scrubber.x = (width - scrubber.width); else scrubber.x = mouseX;
}
/*private function transMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler);
checkPosition();
}*/
private function scrubberMouseDown(event:MouseEvent):void
{
event.stopImmediatePropagation();
TweenMax.killTweensOf(scrubber);
scrubber.startDrag(false, new Rectangle(0, 0, trackLength - scrubber.width, 0));
stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler,false,0,true);
categoryMenu.preTransition();
}
private function stageMouseUpHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler);
scrubber.stopDrag();
checkPosition();
}
private function checkPosition():void
{
var targetX:Number = (trackLength - scrubber.width) >> 1;
if (scrubber.x > (center + 20)) categoryMenu.postTransition(1);
else
if (scrubber.x < (center - 20)) categoryMenu.postTransition(-1);
else
categoryMenu.postTransition(0);
TweenMax.to(scrubber, .5, {x:targetX, ease:Quint.easeOut, overwrite:1});
}
public function nextMenu():void
{
categoryMenu.postTransition(1);
}
public function prevMenu():void
{
categoryMenu.postTransition(-1);
}
}
}
I've inserted the code below in my Main.as to call the function which I can't get it success.
import com.theflashfactor.carouselStackGallery.categoryMenu.CategoryScroller;
private var categoryScroller:CategoryScroller = new CategoryScroller();
categoryScroller.nextMenu();
Main.as
package
{
import com.theflashfactor.carouselStackGallery.CarouselStackGallery;
import com.theflashfactor.carouselStackGallery.categoryMenu.CategoryMenu;
import com.theflashfactor.carouselStackGallery.categoryMenu.CategoryScroller;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import org.casalib.events.LoadEvent;
import org.casalib.load.DataLoad;
/**
* Main document class
* #author Rimmon Trieu
*/
[SWF(frameRate="60", backgroundColor="0", pageTitle="3D Carousel Stack Gallery")]
public class Main extends Sprite
{
private var xmlPath:String = "../xml/Main.xml";
private var ts3:Sprite = new Sprite;
private var categoryMenu:CategoryMenu = new CategoryMenu();
private var categoryScroller:CategoryScroller = new CategoryScroller();
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
addEventListener(Event.ADDED_TO_STAGE, addToStage);
}
private function addToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addToStage);
var xmlLoad:DataLoad = new DataLoad(xmlPath);
xmlLoad.addEventListener(LoadEvent.COMPLETE, xmlLoaded);
xmlLoad.start();
ts3.graphics.beginFill(0x555555,1);
ts3.graphics.drawCircle(0, 0, 50);
ts3.graphics.endFill();
}
private function xmlLoaded(event:LoadEvent):void
{
event.target.removeEventListener(LoadEvent.COMPLETE, xmlLoaded);
opaqueBackground = uint(event.target.dataAsXml.settings.#backgroundColor);
var gallery:CarouselStackGallery = new CarouselStackGallery(event.target.dataAsXml);
addChild(gallery);
addChild(ts3);
ts3.addEventListener(MouseEvent.MOUSE_DOWN, ts3MouseDown,false,0,true);
}
private function ts3MouseDown(event:MouseEvent):void
{
categoryScroller.nextMenu();
}
}
}

As defined:
public function CategoryScroller(categoryMenu:CategoryMenu)
{
...
Shouldn't the class constructor be expecting an argument??
private var categoryScroller:CategoryScroller = new CategoryScroller();

Related

AS3. How can a global var take a value which appears in a listener Event.COMPLETE

This is my code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.LoaderInfo;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.GradientType;
import flash.sampler.getSize;
public class Miniaturka extends MovieClip {
private var id:String;
public static var miniWidth:Number = 0;
private var tween:Tween;
private var tryb:Boolean;
private var button:Sprite;
private var index:Number;
private var aktywna:Boolean = false;
public var bLoad:Boolean = false;
public function Miniaturka(id:String,index:Number):void {
this.id = id;
this.index = index;
tryb = false;
var loader:Loader = new Loader();
loader.load(new URLRequest("images/"+id+"m.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,nLoadComplete);
this.alpha = 1;
button = new Sprite();
button.graphics.beginFill(0x000000,0);
button.graphics.drawRect(0,0,889,500);
button.graphics.endFill();
button.buttonMode = true;
addChild(button);
button.addEventListener(MouseEvent.MOUSE_OVER,onOver);
button.addEventListener(MouseEvent.MOUSE_OUT,onOut);
button.addEventListener(MouseEvent.CLICK,onClick);
}
private function nLoadComplete(event:Event):void {
var loader:Loader = new Loader();
loader = LoaderInfo(event.target).loader;
pusty.addChild(loader);
ladowanie.visible = false;
tween = new Tween(pusty,"alpha",Regular.easeOut,0,0.6,2,true);
bLoad = true;
setStan(false);
miniWidth = loader.width;
pusty.alpha = 0;
}
private function onOver(event:MouseEvent):void {
if (!aktywna) {
setStan(true);
}
}
private function onOut(event:MouseEvent):void {
if (!aktywna) {
setStan(false);
}
}
private function onClick(event:MouseEvent):void {
aktywuj();
}
public function deaktywuj():void {
setStan(false);
aktywna = false;
}
public function aktywuj():void {
MovieClip(parent).deaktywuj();
aktywna = true;
setStan(true);
MovieClip(parent.parent).loadBig(id,index);
}
private function setStan(tryb:Boolean):void {
this.tryb = tryb;
if (tryb) {
pusty.alpha = 1;
} else {
pusty.alpha = 0.6;
}
}
}
}
I want to create a gallery, and this is a code of a class which loads jpg. files with different widths, but the same height.
My problem is that I want to make the public static var miniWidth which takes the value: loader.width in function: nLoadComplete, take that value as a global var, and put it in the line: button.graphics.drawRect(0,0,889,500); so it would look like button.graphics.drawRect(0,0,miniWidth ,500);
This will create a button(rectangle) the same height and width as the loaded jpg. but i can't figure it out... How can I do that?
Wait for the image to load before drawing your shape. In the example below, I've only reprinted the affected functions.
private function Miniaturka(id:String, index:Number):void {
this.id = id;
this.index = index;
tryb = false;
var loader:Loader = new Loader();
loader.load(new URLRequest("images/" + id + "m.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, nLoadComplete);
this.alpha = 1;
}
private function nLoadComplete(event:Event):void {
var loader:Loader = new Loader();
loader = LoaderInfo(event.target).loader;
pusty.addChild(loader);
ladowanie.visible = false;
tween = new Tween(pusty, "alpha", Regular.easeOut, 0, 0.6, 2, true);
bLoad = true;
setStan(false);
miniWidth = loader.width;
pusty.alpha = 0;
createBtn();
}
private function createBtn():void {
button = new Sprite();
button.graphics.beginFill(0x000000, 0);
button.graphics.drawRect(0, 0, miniWidth, 500);
button.graphics.endFill();
button.buttonMode = true;
addChild(button);
button.addEventListener(MouseEvent.MOUSE_OVER, onOver);
button.addEventListener(MouseEvent.MOUSE_OUT, onOut);
button.addEventListener(MouseEvent.CLICK, onClick);
}

ActionScript 3: Zoom functions - drag does not show

I've included a zoom functionality similar to the one explained at this website: http://www.flashandmath.com/howtos/zoom/
Even though it does indeed work in terms of the possibility of moving the image on my stage, the drag-animation is not present, meaning there will be no "movement" of my image, just a sudden change in position.
The problem followed my attempt to change the concept from timeline-based to class-based.
Here is my Main class:
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;
public class Main extends Sprite
{
public static var scale:Number = 1;
private var _rootMC:MovieClip;
// ------------------------------
public var bg_image:Sprite;
public var spImage:Sprite;
public var mat:Matrix;
public var mcIn:MovieClip;
public var mcOut:MovieClip;
public var boardWidth:int = 980;
public var boardHeight:int = 661;
public var boardMask:Shape;
public var externalCenter:Point;
public var internalCenter:Point;
public static var scaleFactor:Number = 0.8;
public static var minScale:Number = 0.25;
public static var maxScale:Number = 10.0;
//-------------------------------
public function Main(rootMC:MovieClip)
{
_rootMC = rootMC;
bg_image = new image();
this.graphics.beginFill(0xB6DCF4);
this.graphics.drawRect(0,0,boardWidth,boardHeight);
this.graphics.endFill();
spImage = new Sprite();
this.addChild(spImage);
boardMask = new Shape();
boardMask.graphics.beginFill(0xDDDDDD);
boardMask.graphics.drawRect(0,0,boardWidth,boardHeight);
boardMask.graphics.endFill();
boardMask.x = 0;
boardMask.y = 0;
this.addChild(boardMask);
spImage.mask = boardMask;
minScale = boardWidth / bg_image.width;
mcIn = new InCursorClip();
mcOut = new OutCursorClip();
bg_image.addChild(mcIn);
bg_image.addChild(mcOut);
bg_image.scaleX = minScale;
bg_image.scaleY = minScale;
spImage.addChild(bg_image);
spImage.addChild(mcIn);
spImage.addChild(mcOut);
spImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
_rootMC.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
spImage.addEventListener(MouseEvent.CLICK, zoom);
_rootMC.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
_rootMC.stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
}
private function startDragging(mev:MouseEvent):void
{
spImage.startDrag();
}
private function stopDragging(mev:MouseEvent):void
{
spImage.stopDrag();
}
private function zoom(mev:MouseEvent):void
{
if ((!mev.shiftKey)&&(!mev.ctrlKey))
{
return;
}
if ((mev.shiftKey)&&(mev.ctrlKey))
{
return;
}
externalCenter = new Point(spImage.mouseX,spImage.mouseY);
internalCenter = new Point(bg_image.mouseX,bg_image.mouseY);
if (mev.shiftKey)
{
bg_image.scaleX = Math.max(scaleFactor*bg_image.scaleX, minScale);
bg_image.scaleY = Math.max(scaleFactor*bg_image.scaleY, minScale);
}
if (mev.ctrlKey)
{
bg_image.scaleX = Math.min(1/scaleFactor*bg_image.scaleX, maxScale);
bg_image.scaleY = Math.min(1/scaleFactor*bg_image.scaleY, maxScale);
}
mat = this.transform.matrix.clone();
MatrixTransformer.matchInternalPointWithExternal(mat,internalCenter,externalCenter);
bg_image.transform.matrix = mat;
}
private function keyHandler(ke:KeyboardEvent):void
{
mcIn.x = spImage.mouseX;
mcIn.y = spImage.mouseY;
mcOut.x = spImage.mouseX;
mcOut.y = spImage.mouseY;
mcIn.visible = ke.ctrlKey;
mcOut.visible = ke.shiftKey;
if (ke.ctrlKey || ke.shiftKey)
{
Mouse.hide();
}
else
{
Mouse.show();
}
}
}
}
Here is my image class:
package {
import fl.motion.MatrixTransformer;
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.MouseEvent;
public class image extends MovieClip
{
public function image()
{
this.width = 980
this.height = 500
this.y = 0
this.x = 0
}
}
I know this is a lot of code, but I am really stuck :(
On your MOUSE_DOWN handler, you need to start listening to the MOUSE_MOVE event.
On your MOUSE_UP handler, you need to stop listening to the MOUSE_MOVE event.
Not sure if you are already doing this.
In your MOUSE_MOVE event handler, you need to update the x / y positions of the image, in a similar way to what you are probably doing on the MOUSE_UP handler.

as3 hitTestPoint detecting alpha

I am trying to make maze where the user draws with mouse and if they hit the wall it erases the line they just drew. I have a png file with alpha that creates the walls of the maze.
I need the user to draw on the alpha but when they hit a non alpha it will trigger an action and erase the line.
Here is the line I am having issues with:
if (myshape.hitTestPoint(theBall.x,theBall.y, true))
Here is the full code:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.*;
public class MazeClass extends Sprite
{
//Are we drawing or not?
private var drawing:Boolean;
public var myshape:Shape;
public var alreadyDrawn:Shape;
public var theBall:Ball = new Ball();
//alreadyDrawn = new Shape();
public function MazeClass()
{
if (stage)
{
myshape = new Shape();
myshape.graphics.lineStyle(12,0x000000);
addChild(myshape);
drawing = false;//to start with
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
stage.addEventListener(MouseEvent.MOUSE_MOVE, draw);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
//stage.addEventListener(Event.ENTER_FRAME, checkIt);
addChild(theBall);
}
}
public function startDrawing(event:MouseEvent):void
{
myshape.graphics.moveTo( mouseX, mouseY);
drawing = true;
}
public function draw(event:MouseEvent)
{
if (drawing)
{
//checkIt();
myshape.graphics.lineTo(mouseX,mouseY);
if (myshape.hitTestPoint(theBall.x,theBall.y, true))
{
trace("Hit A WALL!");
myshape.graphics.clear();
myshape.graphics.lineStyle(12, 0xFFFFFF);
myshape.graphics.moveTo(mouseX,mouseY);
}
}
}
public function stopDrawing(event:MouseEvent)
{
drawing = false;
}
}
}
Another option is to test the colour of the map bitmapdata pixel that corresponds to the "player's position" (the end of the drawn line, in this case):
var testPixel:uint = _myBitmapData.getPixel(xPosition, yPosition);
Or you can use .getPixel32 which includes alpha in the returned uint.
I FOUND A WORKING SOLUTION:
I found a working solution thanks to fsbmain. Thank you. I am posting my full code in hopes that this can help someone else.
In the end I had to use another movieClip rather than the mouse, but this worked for me. Now instead of drawing anywhere in the maze they have to drag a little character named Beau thru the maze. This works better for me.
It would be nice to know how to also detect if the mouse or the actual line I am drawing hit the wall as well. Maybe someone can make a suggestion. Nevertheless, thanks for helping me get this far.
The short and essential code to make my movieClip detect the alpha can be found here. It really is very short compared to other complicated options I found. Here is the link: http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.*;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.desktop.NativeApplication;
import flash.system.Capabilities;
import flash.display.*;
import flash.geom.*;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
public class MazeClass extends Sprite
{
//Are we drawing or not?
private var drawing:Boolean;
public var myshape:Shape;
public var alreadyDrawn:Shape;
public var theMap:*;
public var swiper:Swiper = new Swiper();
public var Beau:littleBeau = new littleBeau();
//alreadyDrawn = new Shape();
public var currentGalleryItem:Number = 1;
public var totalGalleryItems:Number = 4;
public var redClipBmpData:BitmapData;
public var blueClipBmpData:BitmapData;
public var redRect:Rectangle;
public var blueRect:Rectangle;
public function MazeClass()
{
if (stage)
{
theMap = new MapOne();
myshape = new Shape();
myshape.graphics.lineStyle(33,0x0aa6df);
addChild(myshape);
drawing = false;//to start with
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
stage.addEventListener(MouseEvent.MOUSE_MOVE, draw);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
//stage.addEventListener(Event.ENTER_FRAME, checkIt);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_OptionsMenuHandler);
Multitouch.inputMode = MultitouchInputMode.GESTURE;
addChild(theMap);
//theMap.height = stage.stageHeight - 200;
theMap.theStart.alpha = 0;
theMap.theFinish.alpha = 0;
addChild(swiper);
swiper.y = stage.stageHeight - swiper.height;
swiper.addEventListener(TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrame);
addChild(Beau);
Beau.x = theMap.theStart.x;
Beau.y = theMap.theStart.y - swiper.height;
Beau.addEventListener(MouseEvent.MOUSE_DOWN, BeauStartDrag);
Beau.addEventListener(MouseEvent.MOUSE_UP, BeauStopDrag);
redRect = theMap.getBounds(this);
redClipBmpData = new BitmapData(redRect.width,redRect.height,true,0);
redClipBmpData.draw(theMap);
blueRect = Beau.getBounds(this);
blueClipBmpData = new BitmapData(blueRect.width,blueRect.height,true,0);
blueClipBmpData.draw(Beau);
//blueClipBmpData.x = theMap.theStart.x;
//blueClipBmpData.y = theMap.theStart.y - swiper.height;
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
}
}
public function enterFrame(e:Event):void
{
//Beau.x = mouseX;
//Beau.y = mouseY;
if (redClipBmpData.hitTest(new Point(theMap.x, theMap.y),
255,
blueClipBmpData,
new Point(Beau.x, Beau.y),
255
))
{
trace("hit");
clearAll();
//redClip.filters = [new GlowFilter()];
}
else
{
trace("No Hit");
}
}
public function BeauStartDrag(event:MouseEvent):void
{
Beau.startDrag();
drawing = true;
}
public function BeauStopDrag(event:MouseEvent):void
{
drawing = false;
Beau.stopDrag();
}
public function startDrawing(event:MouseEvent):void
{
myshape.graphics.moveTo( mouseX, mouseY);
}
//drawing = true;
public function draw(event:MouseEvent)
{
if (drawing)
{
//checkIt();
myshape.graphics.lineTo(mouseX,mouseY);
}
}
public function stopDrawing(event:MouseEvent)
{
//drawing = false;
}
public function fl_OptionsMenuHandler(event:KeyboardEvent):void
{
if ((event.keyCode == 95) || (event.keyCode == Keyboard.MENU))
{
NativeApplication.nativeApplication.exit(0);
}
}
public function clearAll()
{
myshape.graphics.clear();
myshape.graphics.lineStyle(12, 0x0aa6df);
myshape.graphics.moveTo(mouseX,mouseY);
Beau.x = theMap.theStart.x;
Beau.y = theMap.theStart.y - swiper.height;
drawing = false;
Beau.stopDrag();
}
public function fl_SwipeToGoToNextPreviousFrame(event:TransformGestureEvent):void
{
if (event.offsetX == 1)
{
if (currentGalleryItem > 1)
{
currentGalleryItem--;
trace("swipe Right");
clearAll();
removeChild(theMap);
theMap = new MapOne();
addChild(theMap);
theMap.height = stage.stageHeight - 200;
theMap.theStart.alpha = 0;
theMap.theFinish.alpha = 0;
addChild(Beau);
Beau.x = theMap.theStart.x;
Beau.y = theMap.theStart.y - swiper.height;
}
}
else if (event.offsetX == -1)
{
if (currentGalleryItem < totalGalleryItems)
{
currentGalleryItem++;
trace("swipe Left");
clearAll();
removeChild(theMap);
theMap = new MapTwo();
addChild(theMap);
theMap.height = stage.stageHeight - 200;
theMap.theStart.alpha = 0;
theMap.theFinish.alpha = 0;
addChild(Beau);
Beau.x = theMap.theStart.x;
Beau.y = theMap.theStart.y - swiper.height;
}
}
}
}
}

ActionScript - Problem moving scrollRect with cacheAsBitmap

in order to increase performance of a scrollRect i must cache the vector as a bitmap, otherwise the scrollRect will be simply a less performant mask (info source).
however, i can't seem to move an object/scrollRect once i've applied cacheAsBitmap. why?
package
{
//Imports
import flash.display.Screen;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
//Class
[SWF(width = "800", height = "500", backgroundColor = "0x444444")]
public class ScrollRectTest extends Sprite
{
//Variables
private var background:Sprite;
private var ball:Sprite;
private var newScrollRect:Rectangle;
//Constructor
public function ScrollRectTest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.frameRate = 60;
init();
}
//Initialize
private function init():void
{
background = new Sprite();
background.graphics.beginFill(0x000000, 1.0);
background.graphics.drawRect(0, 0, 200, 400);
background.graphics.endFill();
ball = new Sprite();
ball.graphics.beginFill(0xFF0000, 1.0);
ball.graphics.drawCircle(0, 0, 100);
ball.graphics.endFill();
//ball.cacheAsBitmap = true; //<-- uncomment this
ball.scrollRect = new Rectangle(background.x, background.y, background.width, background.height);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
addChild(background);
addChild(ball);
}
//Mouse Down Event Handler
private function mouseDownEventHandler(evt:MouseEvent):void
{
addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
//Mouse Up Event Handler
private function mouseUpEventHandler(evt:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
//Enter Frame Event Handler
private function enterFrameEventHandler(evt:Event):void
{
newScrollRect = ball.scrollRect;
newScrollRect.y -= 10;
newScrollRect.x -= 5;
ball.scrollRect = newScrollRect;
}
}
}
Even more funny:
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
[SWF(width = "800", height = "500", backgroundColor = "0x444444")]
public class ScrollRectTest extends Sprite
{
//Variables
private var background:Sprite;
private var ball:Sprite;
private var newScrollRect:Rectangle;
//Constructor
public function ScrollRectTest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.frameRate = 60;
init();
}
//Initialize
private function init():void
{
background = new Sprite();
background.graphics.beginFill(0x000000, 1.0);
background.graphics.drawRect(0, 0, 200, 400);
background.graphics.endFill();
ball = new Sprite();
ball.graphics.beginFill(0xFFFF00, 1.0);
ball.graphics.drawCircle(0, 0, 100);
ball.graphics.endFill();
var foo:Sprite = new Sprite();
var g:Graphics = foo.graphics;
g.beginFill(0xFF0000, 0.2);
g.drawRect(0, 0, 100, 100);
g.endFill();
ball.addChild(foo);
ball.cacheAsBitmap = true; //<-- uncomment this
ball.scrollRect = new Rectangle(background.x, background.y, background.width, background.height);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
addChild(background);
addChild(ball);
}
//Mouse Down Event Handler
private function mouseDownEventHandler(evt:MouseEvent):void
{
addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
//Mouse Up Event Handler
private function mouseUpEventHandler(evt:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
//Enter Frame Event Handler
private function enterFrameEventHandler(evt:Event):void
{
newScrollRect = ball.scrollRect;
newScrollRect.y -= 1;
newScrollRect.x -= 1;
ball.scrollRect = newScrollRect;
// ball.scaleX = 1 + Math.random() * 0.01;// uncomment this to force redraw
}
}
}
So I assume it's some kind of bug.

How to mask image with another image in ActionScript 3.0

my issue is this, my users import an image with
FileReference and I need to mask it and then send it to server.
My problem is this: I'm be able do keep the filereference event and
transfer the image data into my canvas. I'm be able to send to server
the result of masking.
But I'm NOT be able to mask the image that my users have load in
my canvas.
You just need to add loaded bitmap to maskable container (e.g. Sprite). For exmaple:
Test.as
package {
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
public class Test extends Sprite {
private var _fileReference:FileReference;
private var _fileFilter:FileFilter;
private var _loader:Loader;
private var _imageContainer:Sprite;
private var _mask:Sprite;
private var _canvas:Sprite;
public function Test() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
}
private function addedToStageListener(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
_fileReference = new FileReference();
_fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
_fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);
_fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
_loader = new Loader();
_canvas = new Sprite();
_mask = new Sprite();
var maskGraphics:Graphics = _mask.graphics;
maskGraphics.beginFill(0xFFFFFF);
maskGraphics.drawCircle(50, 50, 50);
maskGraphics.endFill();
_imageContainer = new Sprite();
_imageContainer.mask = _mask;
_canvas.addChild(_imageContainer);
_canvas.addChild(_mask);
addChild(_canvas);
stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
}
private function mouseClickListener(event:Event):void {
_fileReference.browse([_fileFilter]);
}
private function fileSelectedListener(event:Event):void {
_fileReference.load();
}
private function fileLoadCompleteListener(event:Event):void {
_loader.loadBytes(event.target.data);
while(_imageContainer.numChildren) {
_imageContainer.removeChildAt(0);
}
_imageContainer.addChild(_loader);
}
}}
This changes the situation, but not dramatically. Check out the modified solution:
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileFilter;
import flash.net.FileReference;
public class Test extends Sprite {
private var _fileReference:FileReference;
private var _fileFilter:FileFilter;
private var _imageLoader:Loader;
private var _maskLoader:Loader;
private var _canvas:Sprite;
private var _imageLoaded:Boolean = false;
public function Test() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
}
private function addedToStageListener(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
_fileReference = new FileReference();
_fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
_fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);
_fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
_imageLoader = new Loader();
_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadCompleteListener, false, 0, true);
_maskLoader = new Loader();
_maskLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, maskLoadCompleteListener, false, 0, true);
_canvas = new Sprite();
addChild(_canvas);
stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
}
private function mouseClickListener(event:Event):void {
_fileReference.browse([_fileFilter]);
}
private function fileSelectedListener(event:Event):void {
_fileReference.load();
}
private function fileLoadCompleteListener(event:Event):void {
if (!_imageLoaded) {
_imageLoader.loadBytes(event.target.data);
} else {
_maskLoader.loadBytes(event.target.data);
}
}
private function imageLoadCompleteListener(event:Event):void {
_imageLoaded = true;
}
private function maskLoadCompleteListener(event:Event):void {
var imageBitmap:Bitmap = _imageLoader.content as Bitmap;
var maskBitmap:Bitmap = _maskLoader.content as Bitmap;
if (imageBitmap && maskBitmap) {
var imageBitmapData:BitmapData = imageBitmap.bitmapData;
var maskBitmapData:BitmapData = maskBitmap.bitmapData;
var boundRectange:Rectangle = new Rectangle(0, 0, maskBitmap.width, maskBitmap.height);
var destinationPoint:Point = new Point(0, 0);
var finalBitmapData:BitmapData = new BitmapData(maskBitmap.width, maskBitmap.height);
finalBitmapData.copyPixels(imageBitmapData, boundRectange, destinationPoint, maskBitmapData, destinationPoint);
var finalBitmap:Bitmap = new Bitmap(finalBitmapData);
while(_canvas.numChildren) {
_canvas.removeChildAt(0);
}
_canvas.addChild(finalBitmap);
}
}
}}
First click on the stage allows you to select masked image. Second click allows you to select image, which contains the masking information (e.g. png file with transparency). Hope this helps.