Below is the code of my slideshow, the problem I am having is that after 1st image that loads has the perfect size which I needs but the second image loads has different size I want it to be of same size as of 1st one. Also the images are not loading in a sequence. Can any expert figure out and solve the problem.
stop();
import gs.*;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//hides the description box until the image is loaded
//hides the image until it is loaded
theImage.alpha=0;
loadingBar.visible = false;
//variables to hold the final coordinates of the image tween
var finalX:Number;
var finalY:Number;
//variable to hold the number of images in the XML
var listLength:Number;
//keeps track of what image should be displayed
var currPainting:Number=0;
//arrays to hold the contents of the XML, using this to allow
//for the random order of the images
var imageArray:Array = new Array();
//Loader event for the XML
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);
var xml:XML;
loader.load(new URLRequest("paintings.xml"));
function onLoaded(e:Event):void {
//load XML
xml=new XML(e.target.data);
var il:XMLList=xml.images;
listLength=il.length();
populateArray();
}
function populateArray():void {
//takes the properties defined in the XML and stores them
//into arrays
var i:Number;
for (i = 0; i < listLength; i++) {
imageArray[i]=xml.images[i].pic;
}
beginImage();
}
function beginImage():void {
//grabs a random number between 0 and the number
//of images in the array
currPainting=Math.floor(Math.random()*imageArray.length);
//load description
var imageLoader = new Loader();
//catches errors if the loader cannot find the URL path
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, catchFunction);
//actually loads the URL defined in the image array
imageLoader.load(new URLRequest(imageArray[currPainting]));
//adds a listener for while the image is loading
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imgLoading);
//adds a listener for what to do when the image is done loading
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
function catchFunction(e:IOErrorEvent) {
trace("Bad URL: " + imageArray[currPainting] + " does not exist");
//take out the bad URL from the array
imageArray.splice(currPainting,1);
//check to see if there are images left,
//else restart the slideshow
if (imageArray.length==0) {
populateArray();
} else {
beginImage();
}
}
function imgLoading(event:ProgressEvent):void{
//show the loading bar, and update the width
//based on how much is loaded
loadingBar.visible = true;
loadingBar.x = stage.stageWidth/2;
loadingBar.y = stage.stageHeight/3
loadingBar.bar.width = (event.bytesLoaded/event.bytesTotal)*100;
}
function imgLoaded(event:Event):void {
loadingBar.visible = false;
var scale:Number = stage.stageWidth * 0.292708 / theImage.width;
//add the image and get the dimensions to center the image
theImage.addChild(imageLoader);
theImage.x = stage.stageWidth/2.85;
theImage.y = 0;
if(theImage.height * scale > stage.stageHeight){
scale = stage.stageHeight * 0.704918 / theImage.height;
}
// apply the scale to the image
theImage.scaleX = theImage.scaleY = scale;
//take the contents of the loaded image and cast it as bitmap data
//to allow for bitmap smoothing
var image:Bitmap = imageLoader.content as Bitmap;
image.smoothing=true;
//start tween function
easeIn();
}
}
function easeIn():void {
TweenLite.to(theImage, 8, {onComplete:hideStuff});
TweenLite.to(theImage, 1, {alpha:1, overwrite:0});
}
function hideStuff():void {
TweenLite.to(theImage, 1, {alpha:0, onComplete:nextImage});
}
function nextImage():void {
//take out the image that was just displayed
imageArray.splice(currPainting,1);
//remove the picture
theImage.removeChildAt(0);
//start over
if (imageArray.length==0) {
populateArray();
} else {
beginImage();
}
}
Here is the xml
<xml>
<images><pic>portfolio1.jpg</pic></images>
<images><pic>portfolio2.jpg</pic></images>
<images><pic>portfolio3.jpg</pic></images>
<images><pic>portfolio4.jpg</pic></images>
<images><pic>portfolio5.jpg</pic></images>
</xml>
I don't think theImage's height represents image height after loading. That should be available with loader. To accommodate for different sized images, you'd have to bring actual height/width of image while applying scale. Else, of course, you can pre-resize all the images to same size.
About random loading, that's exactly what
currPainting=Math.floor(Math.random()*imageArray.length);
in beginImage is doing. To have the loading done in order, you can pick up 0th element directly.
Related
I was trying to make a flash app with AS3 where I can draw line with pen tool with different colors , also fill the shapes on an image with different colors, now I have went through various tutorials and achieved it, however in the end I am faced with 2 problems that I am unable to solve even after 3 days of efforts:
How can I fill color inside the shapes formed by using the pen tool,
say if I draw a rough circle using pen tool and then I try and fill
it with green, how can I detect MovieClips which I need to fill.
When I draw lines over shapes and then try and fill the shapes, the
shapes gets filled but the lines still appear on top of the shapes
filled with color.
You can get a better idea of what I have achieved by visiting this link, click the pen symbol and paint bucket symbol to see how it works.
Below is my code for pen tool and fill color:
I draw a sprite add an image and then use the property to detect color to draw a line of color I choose, followed by code to fill color where I divide the image in various MovieClips and then make then into one and detect if mouse is clicked on which clip and fill it with selected color.
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
bucbut.addEventListener(MouseEvent.CLICK,nClick0PC);
/////////////pentool code --------
convertToBMD();
pbut.addEventListener(MouseEvent.CLICK,nClick0P);
function nClick0P(event:MouseEvent):void{
spBoard.addEventListener(MouseEvent.ROLL_OUT,boardOut);
spBoard.addEventListener(MouseEvent.MOUSE_MOVE,boardMove);
spBoard.addEventListener(MouseEvent.MOUSE_DOWN,boardDown);
spBoard.addEventListener(MouseEvent.MOUSE_UP,boardUp);
}
var spBoard:Sprite=new Sprite();
this.addChildAt(spBoard,0);
spBoard.x=20;
spBoard.y=100;
var owl2:owl;
owl2 = new owl();
owl2.name="owl1";
spBoard.addChildAt(owl2,0);
owl2.x=315;
owl2.y=180;
var shDrawing:MovieClip = new MovieClip();
//var shDrawing:Shape=new Shape();
spBoard.addChild(shDrawing);
//spBoard.addChildAt(shDrawing,1);
function nClick0PC(event:MouseEvent):void{
owl2.addEventListener(MouseEvent.CLICK,on_owl_click);
}
var doDraw:Boolean=false;
var lineSize:Number=10;
var currentColor:Number;
spBoard.graphics.lineStyle(1,0x000000);
spBoard.graphics.beginFill(0xFFFFFF);
spBoard.graphics.drawRect(0,0,602,330);
spBoard.graphics.endFill();
spBoard.filters = [ new DropShadowFilter() ];
function boardOut(e:MouseEvent):void {
doDraw=false;
}
function boardDown(e:MouseEvent):void {
doDraw=true;
trace(activeColor);
shDrawing.graphics.lineStyle(lineSize,activeColor);
shDrawing.graphics.endFill();
shDrawing.graphics.moveTo(shDrawing.mouseX,shDrawing.mouseY);
}
function boardUp(e:MouseEvent):void {
doDraw=false;
}
function boardMove(e:MouseEvent):void {
var curX:Number=shDrawing.mouseX;
var curY:Number=shDrawing.mouseY;
if(doDraw && checkCoords(curX,curY)){
shDrawing.graphics.lineTo(curX,curY);
e.updateAfterEvent();
}
}
function checkCoords(a:Number,b:Number):Boolean {
if(a>=605-lineSize/2 || a<=lineSize/2 || b>=311-lineSize/2 || b<=lineSize/2){
return false;
}
else {
return true;
}
}
/////////////---------------------color picker
colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
var pixelValue:uint;
var activeColor:uint = 0x000000;
var ct:ColorTransform = new ColorTransform();
var colorsBmd:BitmapData;
function convertToBMD():void
{
colorsBmd = new BitmapData(colors.width,colors.height);
colorsBmd.draw(colors);
}
function chooseColor(e:MouseEvent):void
{
pixelValue = colorsBmd.getPixel(colors.mouseX,colors.mouseY);
activeColor = pixelValue;//uint can be RGB!
ct.color = activeColor;
//shapeSize.transform.colorTransform = ct;
}
////////////////////========================================Fill color
function on_owl_click(e:MouseEvent):void {
for (var i:int = 0; i < owl2.numChildren; i++) {
if (owl2.getChildAt(i).hitTestPoint(mouseX,mouseY,true)) {
trace(owl2.getChildAt(i).name);
owl2.getChildAt(i).transform.colorTransform= ct;
}
}
}
I deleted a lot of your code and left this:
convertToBMD();
colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
var activeColor: uint = 0x000000;
var colorsBmd: BitmapData;
function convertToBMD(): void
{
colorsBmd = new BitmapData(colors.width, colors.height);
colorsBmd.draw(colors);
}
function chooseColor(e: MouseEvent): void
{
var pixelValue:uint = colorsBmd.getPixel(colors.mouseX, colors.mouseY);
activeColor = pixelValue; //uint can be RGB!
}
Also I removed an owl at the stage. Download my FLA to see changes.
Next. I added two canvases.
var canvasData:BitmapData = new BitmapData(650, 437, false, 0xEFEFEF);
var canvas:Bitmap = new Bitmap(canvasData);
canvas.x = 0;
canvas.y = 102;
addChild(canvas);
var penCanvas:Shape = new Shape();
penCanvas.x = canvas.x;
penCanvas.y = canvas.y;
You can read about Bitmap and BitmapData here.
First canvas it's a raster image. Second canvas it's a Shape, so you can use moveTo and lineTo methods to draw with a pencil.
Next. In library i found an owl image and export it to code.
If not understand, I can explain more detailed.
Next. Registration event handlers.
bucbut.addEventListener(MouseEvent.CLICK, clickBucket);
pbut.addEventListener(MouseEvent.CLICK, clickPen);
function clickBucket(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, canvasDown);
stage.addEventListener(MouseEvent.CLICK, clickOnCanvas);
}
function clickPen(event:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, canvasDown);
stage.removeEventListener(MouseEvent.CLICK, clickOnCanvas);
}
Let's see at clickOnCanvas method:
function clickOnCanvas(event:MouseEvent):void
{
// If we click at the canvas
if (canvas.hitTestPoint(mouseX,mouseY))
{
canvasData.floodFill(canvas.mouseX, canvas.mouseY,activeColor);
}
}
About floodFill you can read here.
And the last three methods used to draw by pen.
function canvasDown(event:MouseEvent):void
{
penCanvas.graphics.lineStyle(10, activeColor);
penCanvas.graphics.moveTo(penCanvas.mouseX, penCanvas.mouseY);
// only when mouse button is down we register two handlers, one for move and another for mouse up
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
}
function mouseMove(event:MouseEvent):void
{
// when mouse is moving we are drawing line
penCanvas.graphics.lineTo(penCanvas.mouseX, penCanvas.mouseY);
// As I said earlier canvasData it's a raster image. So we copy penCanvas and paste to canvasData.
canvasData.draw(penCanvas);
// For a smoother drawing
event.updateAfterEvent();
}
function mouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
}
That's all!
Here you can download sources.
I am new to as3.0 and want to complete my project.
I am using the below code for my xml slideshow but I want the pictures to fade into each other it should never fade to complete white. Hope any expert can help me.
import gs.*;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import fl.transitions.easing.*;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//hides the description box until the image is loaded
//hides the image until it is loaded
theImage.alpha=0;
loadingBar.visible = false;
//variables to hold the final coordinates of the image tween
var finalX:Number;
var finalY:Number;
//variable to hold the number of images in the XML
var listLength:Number;
//keeps track of what image should be displayed
var currPainting:Number=0;
//arrays to hold the contents of the XML, using this to allow
//for the random order of the images
var imageArray:Array = new Array();
//Loader event for the XML
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);
var xml:XML;
loader.load(new URLRequest("galleries/xml/maingallery.xml"));
function onLoaded(e:Event):void {
//load XML
xml=new XML(e.target.data);
var il:XMLList=xml.images;
listLength=il.length();
populateArray();
}
function populateArray():void {
//takes the properties defined in the XML and stores them
//into arrays
var i:Number;
for (i = 0; i < listLength; i++) {
imageArray[i]=xml.images[i].pic;
}
beginImage();
}
function beginImage():void {
//grabs a random number between 0 and the number
//of images in the array
//load description
var imageLoader = new Loader();
//catches errors if the loader cannot find the URL path
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, catchFunction);
//actually loads the URL defined in the image array
imageLoader.load(new URLRequest(imageArray[currPainting]));
//adds a listener for while the image is loading
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imgLoading);
//adds a listener for what to do when the image is done loading
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
function catchFunction(e:IOErrorEvent) {
trace("Bad URL: " + imageArray[currPainting] + " does not exist");
//take out the bad URL from the array
imageArray.splice(currPainting,1);
//check to see if there are images left,
//else restart the slideshow
if (imageArray.length==0) {
populateArray();
} else {
beginImage();
}
}
function imgLoading(event:ProgressEvent):void{
//show the loading bar, and update the width
//based on how much is loaded
loadingBar.visible = true;
loadingBar.bar.scale = (event.bytesLoaded/event.bytesTotal)*100;
loadingBar.x = stage.stageWidth/2;
loadingBar.y = stage.stageHeight - 400;
}
var widthRatio:Number;
var heightRatio:Number;
function imgLoaded(event:Event):void {
loadingBar.visible = false;
//add the image and get the dimensions to center the image
theImage.addChild(imageLoader);
if (imageLoader.width > stage.stageWidth){
widthRatio=imageLoader.width/stage.stageWidth;
trace(widthRatio)
trace(imageLoader.width);
}
if (imageLoader.height > stage.stageHeight - 207){
heightRatio=imageLoader.height/(stage.stageHeight - 207) ;
trace(heightRatio)
trace(imageLoader.height)
}
if (widthRatio > heightRatio){
imageLoader.width = stage.stageWidth;
imageLoader.height = imageLoader.height/widthRatio;
} else {
imageLoader.height = stage.stageHeight - 207;
imageLoader.width = imageLoader.width/heightRatio;
}
imageLoader.x = (imageLoader.stage.stageWidth / 2) - (imageLoader.width / 2);
imageLoader.y = 0
//take the contents of the loaded image and cast it as bitmap data
//to allow for bitmap smoothing
var image:Bitmap = imageLoader.content as Bitmap;
image.smoothing=true;
//start tween function
easeIn();
}
}
function easeIn():void {
TweenLite.to(theImage, 5, {onComplete:hideStuff});
TweenLite.to(theImage, 1, {alpha:1, overwrite:0});
}
function hideStuff():void {
TweenLite.to(theImage, 1, {alpha:0, onComplete:nextImage});
}
function nextImage():void {
//take out the image that was just displayed
imageArray.splice(currPainting,1);
//remove the picture
theImage.removeChildAt(0);
//start over
if (imageArray.length==0) {
populateArray();
} else {
beginImage();
}
}
Ok, first you need to create a variable for the image you wish to fade in. Let's organise it so it is clear which is which.
var theCurrentImage:DisplayObject;
var theNextImage:DisplayObject;
Load in the first image (now theCurrentImage) in the same way as before.
Now here's how you could load in the next image in the array:
function loadNextImage():void {
currPainting++;
// you should also make sure currPainting is not out of bounds here
loadImage();
}
function loadImage():void {
imageLoader = new Loader();
imageLoader.addEventListener(Event.COMPLETE, onImageLoaded);
// other listeners go here too
imageLoader.load(new URLRequest(imageArray[currPainting]));
}
function onImageLoaded(e:Event):void {
theNextImage = imageLoader; // stores the next image ready to be faded in
theNextImage.alpha = 0;
addChild(theNextImage); // now ready to be cross-faded in
crossFade();
}
function crossFade():void {
TweenLite.to(theCurrentImage, 1, {alpha:0});
TweenLite.to(theNextImage, 1, {alpha:1});
// remove theCurrentImage (now invisible) from the stage
removeChild(theCurrentImage);
// theNextImage is now the displayed image so...
theCurrentImage = theNextImage;
theNextImage = null;
// ready to load the next image by calling loadNextImage()
}
This is a script for when i click an object, it opens a small book with some page flip effect.
I'm done with almost everything but i want that when i click in a back button everything desapears and i go back to only seeing the original object. It is not working because its only deleting one of the pages! I tried doing an array but it didnt work either and Im not very good with arrays too. Can anyone help?
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.Sprite;
import flash.display.Loader;
var cont : DisplayObject;
var cont2 : DisplayObject;
var imgLoader : Loader;
//loads pages
for (var i:int=0; i<=4; i++){
imgLoader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(Event.INIT, onLoadJPEG);
imgLoader.load(new URLRequest(""+i+".png"));
}
var imgLoader2 : Loader;
//loads back button
imgLoader2 = new Loader();
imgLoader2.contentLoaderInfo.addEventListener(Event.INIT, onLoadSketch);
imgLoader2.load(new URLRequest("voltaatrassketchbook.png"));
function onLoadJPEG (e : Event) : void {
cont = e.target.loader;
cont.x =250;
cont.y =50;
cont.width = (445-100)/2;
cont.height = (604-100)/2;
addChild(cont);
cont.addEventListener(MouseEvent.MOUSE_UP, FlipPage);
}
function onLoadSketch (e : Event) : void {
cont2 = e.target.loader;
cont2.x =450;
cont2.y =300;
cont2.width = 181/2;
cont2.height = 127/2;
addChild(cont2);
cont2.addEventListener(MouseEvent.MOUSE_UP, volta);
}
function FlipPage(e:MouseEvent):void{
setChildIndex(DisplayObject(e.currentTarget), this.numChildren - 1);
if (e.currentTarget.rotationY == 0) {
var myTween:Tween = new Tween(e.currentTarget, "rotationY",
Regular.easeInOut,0, 180, 1, true);
}
if (e.currentTarget.rotationY == 180) {
var myTween:Tween = new Tween(e.currentTarget, "rotationY",
Regular.easeInOut, 180, 0, 1, true);
}
}
//function to go back
function volta (e: MouseEvent): void {
gotoAndStop(1);
cont.visible=false;
cont2.visible=false;
}
Option 1
You are right that you could use an array. Put this at the top of your code, before you start loading the pages:
var pages:Array = [];
Then put this as the final line inside onLoadJPEG()
pages.push(cont);
That will add each image to the array when it is loaded.
Then in volta() you can loop through the array and make each image invisible
for(var i:int = 0; i < pages.length; i++) {
DisplayObject(pages[i]).visible = false;
}
Option 2
Another approach would be to add all the images to a container Sprite and then all you would have to do is make the container Sprite invisible.
Add this to the top of your code before you load the pages :
var pages:Sprite = new Sprite();
addChild(pages);
Then in onLoadJPEG() add cont as a child of the container
pages.addChild(cont);
Then in volta() :
pages.visible = false;
If you use this approach, don't forget to call setChildIndex() on the container inside of FlipPage() :
pages.setChildIndex(DisplayObject(e.currentTarget), this.numChildren - 1);
i actually try to do the following: I have loaded an external image in a bitmapdata object and create a bitmap from it which i attach it to a sprite/MovieClip in order to have mouse events on it. Now under the previous logic i loaded two images (let's say circles) of the same size one that has a particular color and is covered by its black foreground circle. When i press left mouse button and hold it down i want while the mouse is moved to erase the foreground circle's pixels and so the background image starting to appear. I tried this to achieve but had no luck. In my best attempt i achieve to draw a line in the foreground image but i cannot reveal the background!
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.BlendMode;
public class Test2 extends MovieClip
{
// properties - state
// to attach the image and have mouse events
private var frontImage:Sprite;
private var backImage:Sprite;
// to load the image
private var myLoader:Loader;
// to get the bitmap data of the image
private var frontBitmapData:BitmapData;
private var frontBitmap:Bitmap;
// test
private var frontMask:Bitmap;
// constructor
function Test2():void
{
// load the background image
backImage = new Sprite();
attachImageToSprite1(new URLRequest("btest.jpg"));
backImage.mouseEnabled = false;
this.addChild( backImage );
// load the front image
frontImage = new Sprite();
attachImageToSprite2(new URLRequest("test.jpg"));
frontImage.mouseEnabled = true; // enable mouse
frontImage.buttonMode = true; // set button mode
this.addChild(frontImage); // load to stage
this.frontImage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
this.frontImage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
// methods
private function attachImageToSprite1(Name:URLRequest):void
{
this.myLoader = new Loader();
this.myLoader.load(Name);
this.myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete1);
}
private function attachImageToSprite2(Name:URLRequest):void
{
this.myLoader = new Loader();
this.myLoader.load(Name);
this.myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete2);
}
private function getImageBitmapDataFromSprite(srcImage:Sprite):BitmapData
{
var tmpBitmapData:BitmapData = new BitmapData(frontImage.width, frontImage.height, true, 0xFFCCCCCC);
tmpBitmapData.lock();
tmpBitmapData.draw(frontImage);
tmpBitmapData.unlock();
return tmpBitmapData;
}
private function isPixelAlpha(bitmapdata:BitmapData):Boolean
{
var pixelValue:uint = bitmapdata.getPixel32(mouseX, mouseY);
var alphaValue:uint = pixelValue >> 24 & 0xFF;
//var red:uint = pixelValue >> 16 & 0xFF;
//var green:uint = pixelValue >> 8 & 0xFF;
//var blue:uint = pixelValue & 0xFF;
return (alphaValue == 0x00) ? true : false;
}
private function deletePixelUnderMouse(bitmapdata:BitmapData, bitmap:Bitmap):void
{
bitmapdata.lock();
if ( !isPixelAlpha(bitmapdata) ) {
bitmapdata.setPixel32(mouseX, mouseY, 0xFF << 24); // how to make the current pixel's alpha
} // equal to zero.
bitmap = new Bitmap(bitmapdata);
bitmap.x = frontImage.x;
bitmap.y = frontImage.y;
this.frontImage.addChild(bitmap);
bitmapdata.unlock();
}
// events
public function onLoadComplete1(e:Event):void
{
frontImage.addChild(this.myLoader.content);
}
public function onLoadComplete2(e:Event):void
{
backImage.addChild(this.myLoader.content);
}
public function onMouseDown(e:MouseEvent):void
{
// delete a pixel from the sprite under the mouse
frontBitmapData = getImageBitmapDataFromSprite(frontImage);
deletePixelUnderMouse(frontBitmapData, frontBitmap);
frontImage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseDown);
trace("start");
}
public function onMouseUp(e:MouseEvent):void
{
frontImage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseDown);
trace("stop")
}
}
}
Not sure if I got it right, but if you want a 'reveal' effect, as in you draw a mask to display a hidden image for example, this could be achieved slightly easier:
var bitmapToReveal:BitmapData = new BitmapToReveal(0,0);
var brush:BitmapData = new Brush(0,0);
var canvasData:BitmapData = new BitmapData(bitmapToReveal.width,bitmapToReveal.height,true,0x00FFFFFF);
var cursor:Point = new Point();//used as destination point when painting
var zero:Point = new Point();//reused for painting
var reveal:Bitmap = new Bitmap(bitmapToReveal);
var canvas:Bitmap = new Bitmap(canvasData);
reveal.cacheAsBitmap = canvas.cacheAsBitmap = true;
addChild(reveal);
addChild(canvas);
reveal.mask = canvas;
stage.addEventListener(MouseEvent.MOUSE_DOWN, brushDown);
stage.addEventListener(MouseEvent.MOUSE_UP, brushUp);
function brushDown(event:MouseEvent):void {
this.addEventListener(Event.ENTER_FRAME, paint);
}
function brushUp(event:MouseEvent):void {
this.removeEventListener(Event.ENTER_FRAME, paint);
}
function paint(event:Event):void {
cursor.x = mouseX-brush.width*.5;
cursor.y = mouseY-brush.height*.5;
canvasData.copyPixels(brush,brush.rect,cursor,brush,zero,true);
}
I'm using two Bitmaps form the library(bitmapToReveal and brush).
The main thing to look at is the copyPixels() method. I copy
the brush bitmap into the canvas(an empty transparent bitmap data),
using the offset cursor position(so the brush centered), and using the
alpha channel to do that. Note that I've set cacheAsBitmap to true
for both mask and maskee. You need to do that to get a transparent mask,
which is key to the effect.
Here is the result:
You can 'paint' the mask here. CS4 Source is here.
HTH,
George
The preloader does not show up after 3% like it should have, it shows up when the file has loaded entirely.
Can someone help explain to me what I am doing wrong? My code is in the first frame, and it makes use of a rectangle object, and a textfield object. In other preloaders I have seen with code like this, it uses a movieclip with 100 frames. Does that make the difference? I have code updating the width of the rectangle, and something to update the text in the dynamic textbox as well.
My entire code in the first frame:
import flash.display.MovieClip;
import flash.events.ProgressEvent;
function update(e:ProgressEvent):void {
//trace(e.bytesLoaded);
if (loader) {
loader.text = Math.round(e.bytesLoaded*100/e.bytesTotal).toString() + " %";
}
if (bar) {
bar.width = Math.round(e.bytesLoaded*100/e.bytesTotal)*2;
}
}
loaderInfo.addEventListener(ProgressEvent.PROGRESS, update);
var loader:TextField = new TextField();
var bar:preloader_bar = new preloader_bar();
addEventListener(Event.ENTER_FRAME, checkFrame);
var loaderTextFormat:TextFormat = new TextFormat("_sans", 16, 0x000000, true);
loaderTextFormat.align = TextFormatAlign.CENTER;
loader.defaultTextFormat = loaderTextFormat;
bar.color = 0x000000;
addChild(bar);
addChild(loader);
// Extra test for IE
var percent:Number = Math.floor( (this.loaderInfo.bytesLoaded*100)/this.loaderInfo.bytesTotal );
if (percent == 100) {
nextFrame();
}
stop();
if (loader) {
loader.x = (stage.stageWidth - loader.width) / 2;
loader.y = stage.stageHeight / 2;
}
if (bar) {
bar.x = (stage.stageWidth - 200) / 2;
bar.y = (stage.stageHeight - bar.height) / 2;
}
function checkFrame(e:Event):void {
if (currentFrame == totalFrames) {
removeEventListener(Event.ENTER_FRAME, checkFrame);
startup();
}
}
function startup():void {
// hide loader
stop();
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, update);
var mainClass:Class = Main as Class;
addChild(new mainClass() as DisplayObject);
}
It really should be showing up, is there some fancy export option I need to change? I tried this with the bandwidth profiler, it only shows anything after the 100% mark.
EDIT: progress_bar is a movieclip which was exported for actionscript.
You problem seem very similar to this.
Short version: Do you have a single frame ?
If so, move as much as you can on the 2nd frame and also
set that as the Export Frame for actionscript.
Once your first frame has a small size, you will see the preloader easily.
HTH,
George