looping custom cursor opacity falling in infinite loop - actionscript-3

I am trying to make my custom cursor glow slowly by changing its opacity. The problem is that the only way I can see it working is by making a while, which creates an infinite loop. Is there a way to proceed so my cursor changes from 0 opacity to 1 opacity and go up and down. Here is my code at the moment... I'm trying to find another way to proceed, but I really don't see any other way.
public var Alpha:Number = 1;
public var sense:String = "down";
private function thisMouseOver(e:MouseEvent):void{
Mouse.hide();
//draws the cursor
drawCursor();
//Animates cursor
if(!this.animationStarted)
{
this.animationStarted = true;
animateCursor();
}
}
private function animateCursor():void{
while(this.animationStarted)
{
if(this.Alpha==1)
{
this.sense = "down";
}
else if(this.Alpha == 0)
{
this.sense = "up";
}
if(this.sense == "up")
this.Alpha += 0.1;
else
this.Alpha -= 0.1;
this.graphics.beginFill(0x333333);
this.graphics.drawRect(0,0,25,25);
this.graphics.endFill();
drawCursor();
}
}
private function drawCursor():void{
this.graphics.beginFill(0x00BFFF,this.Alpha);
//top left
this.graphics.drawRect(0,0,6,2);
//bottom left
this.graphics.drawRect(0,23,6,2);
//left top
this.graphics.drawRect(0,0,2,6);
//left bottom
this.graphics.drawRect(0,19,2,6);
//top righ
this.graphics.drawRect(19,0,6,2);
//right top
this.graphics.drawRect(23,0,2,6);
//bottom right
this.graphics.drawRect(19,23,6,2);
//right bottom
this.graphics.drawRect(23,19,2,6);
this.graphics.endFill();
}

The best way is to use tweener, my favorite is GTween:
private function test():void
{
var shape:Shape = new Shape();
addChild(shape);
shape.graphics.beginFill(0xFF0000, 1);
shape.graphics.drawCircle(100, 100, 50);
shape.graphics.endFill();
var tween:GTween = new GTween(shape, 1, {alpha:0}, {reflect:true, repeatCount:2});
tween.nextTween = tween;
}
Use tween.paused = true/false to play()/stop() the tween.
There is another variant without tweener (but I recommend to use tween because code is more clear, readable and it's less resources cost) that is more like your, but instead of while loop use EnterFrame event approach - remove while loop from animateCursor() method, add Event.ENTER_FRAME listener in the point of animateCursor(); call:
if(!this.animationStarted)
{
this.animationStarted = true;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame():void
{
animateCursor();
}

One way to avoid your problem with the infinite loop is to put your animateCursor code inside an ENTER_FRAME event handler.
Here's how it can look: http://www.swfcabin.com/open/1360303185
( Your custom cursor shape looks awesome ; )
Here's your code remade in MiniBuilder ( functions are arranged alphabetically ):
package com.glowingcursor {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.ui.*;
public class Application extends Sprite {
private var cursor:Sprite;
private var cursorOffset:Point;
private var sense:Boolean;
public function
Application() {
addEventListener( Event.ADDED_TO_STAGE, onAdded );
}
private function
animateCursor( e:Event ):void {
if ( cursor.alpha >= 1 ) sense = false;
else if ( cursor.alpha <= 0.2 ) sense = true;
if( sense ) cursor.alpha += 0.08;
else cursor.alpha -= 0.08;
}
private function
hideCursor( e:MouseEvent ):void {
Mouse.show();
removeChild( cursor );
removeEventListener( Event.ENTER_FRAME, moveCursor );
removeEventListener( Event.ENTER_FRAME, animateCursor );
}
private function
initCursor():void {
cursor = new Sprite();
cursor.graphics.beginFill( 0xff0000 );
cursor.graphics.drawRect( 0, 0, 6, 2 ); // top left
cursor.graphics.drawRect( 0, 23, 6, 2 ); // bottom left
cursor.graphics.drawRect( 0, 0, 2, 6 ); // left top
cursor.graphics.drawRect( 0, 19, 2, 6 ); // left bottom
cursor.graphics.drawRect( 19, 0, 6, 2 ); // top right
cursor.graphics.drawRect( 23, 0, 2, 6 ); // right top
cursor.graphics.drawRect( 19, 23, 6, 2 ); // bottom right
cursor.graphics.drawRect( 23, 19, 2, 6 ); // right bottom
cursor.graphics.endFill();
// So InteractiveObjects react to the custom cursor properly
cursor.mouseEnabled = false;
// For cursor centering
cursorOffset = new Point( cursor.width / 2, cursor.height / 2 );
}
private function
moveCursor( e:Event ):void {
cursor.x = mouseX - cursorOffset.x;
cursor.y = mouseY - cursorOffset.y;
}
private function
onAdded( e:Event ):void {
initCursor();
showCursor();
}
private function
showCursor():void {
Mouse.hide();
addChild( cursor );
addEventListener( Event.ENTER_FRAME, moveCursor );
addEventListener( Event.ENTER_FRAME, animateCursor );
}
}
}

You can put your code from the animateCursor inside a different method, and use a Timer to call that method. This way you can control how "fast" the cursor blinks and when.
Timer timer = new Timer( 25, 0 );
private function animateCursor():void
{
timer.addEventListener( TimerEvent.TIMER, timerHandler );
timer.start()
}
private function timerListener( e:TimerEvent ):void
{
if(this.Alpha==1)
{
this.sense = "down";
}
else if(this.Alpha == 0)
{
this.sense = "up";
}
if(this.sense == "up")
this.Alpha += 0.1;
else
this.Alpha -= 0.1;
this.graphics.beginFill(0x333333);
this.graphics.drawRect(0,0,25,25);
this.graphics.endFill();
drawCursor();
}
also it's a good idea to change your alpha conditions from this.Alpha == 0 to this.Alpha <= 0 as you may choose to change the amount by which you modify the alpha to values that don't always add up to 0 or 1.

Related

How to tween text up and down, and make it react to touch events

I have a Tweenlite animation in actionscript and I want to execute a method on Touch Event that would interrupt tweening. Actually it would pause it, execute method and then continue tweening.
I have no idea how to do that. Can someone help?
Thanks!
So, I know this doesn't work. My question is how to make it work.
package {
imports...
public class App extends Sprite {
private var textField:TextField = new TextField(220, 35, "Tap to flip the text!", "Roboto", 22, 0xf1f1f1, false);
public function App() {
super();
addEventListener(Event.ADDED_TO_STAGE, textPlay);
}
private function textPlay(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, textPlay);
textField.border = 1;
textField.hAlign;
textField.vAlign;
textField.x = stage.stageWidth / 2 - textField.width / 2;
textField.y = stage.stageHeight / 2 - textField.height / 2;
addChild(textField);
addEventListener(TouchEvent.TOUCH, turn);
down();
}
private function down():void {
addEventListener(starling.events.Event.ENTER_FRAME, check);
TweenLite.to(textField, 5, {y:(stage.stageHeight - textField.height)});
up();
}
private function up():void {
TweenLite.to(textField, 5, {y:0});
down()
}
private function turn(event:TouchEvent):void {
TweenLite.to(textField, 0, {rotation:180});
}
}
}
you need to store the tween in a variable so you can manipulate it later
var myTween:TweenLite = TweenLite.to(...);
function myTouchEventHandler(e:TouchEvent):void {
myTween.pause(); //pause the tween
}
function someOtherFunction():void {
myTween.resume(); //resume tweening from where it left off
}
EDIT
Here is what I think you're trying to do (which doesn't have anything to do with pausing or stopping tweens)
package {
imports...
public class App extends Sprite {
private var textField:TextField = new TextField(220, 35, "Tap to flip the text!", "Roboto", 22, 0xf1f1f1, false);
public function App() {
super();
addEventListener(Event.ADDED_TO_STAGE, textPlay);
}
private var moveSpeed:int = 2; //how many pixels to move the textField every frame
private function textPlay(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, textPlay);
textField.border = 1;
textField.hAlign;
textField.vAlign;
textField.x = stage.stageWidth / 2 - textField.width / 2;
textField.y = stage.stageHeight / 2 - textField.height / 2;
addChild(textField);
addEventListener(TouchEvent.TOUCH, turn);
//this instead of using a tween, just use the enterframe method and move the textField up (or down) every frame
addEventListener(starling.events.Event.ENTER_FRAME, moveText);
}
private function moveText(event:flash.events.Event):void {
if(textField.y + moveSpeed > (stage.stageHeight - textField.height) || textField.y + moveSpeed < 0){
moveSpeed *= -1; //change direction
};
textField.y += moveSpeed;
}
private function turn(event:TouchEvent):void {
TweenLite.to(textField, 0, {rotation:textField.rotation == 180 ? 0 : 180}); //assuming you want to toggle 180 or 0, not just always set to 180
}
}
}
If you still wanted to use a tween, then this would be the way: (using the onComplete parameter of tweenLite)
function up():void {
TweenLite.to(textField, 5, {y:0, onComplete: down});
}
function down():void {
TweenLite.to(textField, 5, {y:(stage.stageHeight - textField.height), onComplete: up});
}
function turn(event:TouchEvent):void {
//need the overwrite property so it doesn't cancel the other tween on this same object
TweenLite.to(textField, 0, {overwrite: 0, rotation:textField.rotation == 180 ? 0 : 180}); //assuming you want to toggle 180 or 0, not just always set to 180
}
OR, use TweenMax and set the yoyo property to true and just use one single tween.

As3 hittesting doesn't really work (nested)

I got a tank.
I got a hero (controllable character).
The tank has a nested movieclip which has a very thin surface area.
Yet, the thing detects a collision when it's not even touching the hero.
public class tank_sight extends MovieClip
{
private var _root:MovieClip;
public function tank_sight()
{
addEventListener(Event.ADDED, beginClass);
}
private function beginClass(event:Event):void
{
_root = MovieClip(root);
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(event:Event):void
{
if(this.hitTestObject(_root.hero.hitbox))
{
this.gotoAndStop(2);
trace("HIT");
fire();
}
else
{
this.gotoAndStop(1);
}
}
private function fire():void
{
var shell:Shell = new Shell(x, y, rotation - 180);
_root.addChild(shell);
}
}
What's wrong? I don't get it.
EDIT: Sight is rotating, so that's probably why. I tried using this code on the player class:
point = _root.tanks.barrel.sight.localToGlobal(new Point());
if(this.hitTestPoint(point.x, point.y, false))
{
trace("HIT");
}
But it doesn't work.. It never traces "HIT", unless I stand in some weird location at certain times.
hitTestObject works with nested objects also (display objects that have different parents), you should check logic of your game, and do some tests.
Simple example:
var squareHolder:Sprite = new Sprite();
var squareInner:Shape = new Shape();
var hitHolder:Sprite = new Sprite();
var hitCircle:Shape = new Shape();
hitCircle.graphics.beginFill(0x990000);
hitCircle.graphics.drawCircle(0, 0, 20);
squareInner.graphics.beginFill(0x009900);
squareInner.graphics.drawRect(0, 0, 40, 40);
addChild(squareHolder);
squareHolder.addChild(squareInner);
squareHolder.x = 200;
squareHolder.y = 100;
squareInner.x = 50;
squareInner.y = 50;
stage.addChild(hitHolder);
hitHolder.addChild(hitCircle);
hitCircle.transform.matrix = new Matrix(1, 0.5, 0.5, 1, 30, 30);
stage.addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void {
hitHolder.x = e.stageX;
hitHolder.y = e.stageY;
if (hitCircle.hitTestObject(squareInner)) {
trace("Ding!");
}
});
Whatever you do with hitCircle (visible=false, trasparent fill, transformations) it will still work.

AS3 vertical shooter: Mouse click not working

I'm just trying to make a simple vertical shooter, one where the player's ship is controlled by the mouse and fires a laser when you click the mouse. However, when I try running the code, I keep getting the same error message:
"1046: Type was not found or was not a compile-time constant: MouseEvent."
The thing is, I declared the MouseEvent. I know I did. It is as follows:
--==--
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
public class SpaceShooter_II extends MovieClip //The public class extends the class to a movie clip.
{
public var army:Array; //the Enemies will be part of this array.
///*
//Laser Shots and Mouse clicks:
import flash.events.MouseEvent;
public var playerShot:Array; //the player's laser shots will fill this array.
public var mouseClick:Boolean;
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseGoDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseGoUp);
stage.addEventListener(Event.ENTER_FRAME, onTick);
//*/
//Back to the rest of the code:
public var playerShip:PlayerShip; //This establishes a variable connected to the PlayerShip AS.
public var onScreen:GameScreen; //This establishes a variable that's connected to the GameScreen AS.
public var gameTimer:Timer; //This establishes a new variable known as gameTimer, connected to the timer utility.
///*
//Functions connected to Shooting via mouse-clicks:
public function mouseGoDown(event:MouseEvent):void
{
mouseClick = true;
}
public function mouseGoUp(event:MouseEvent):void
{
mouseClick = false;
}
//*/
//This function contains the bulk of the game's components.
public function SpaceShooter_II()
{
//This initiates the GameScreen.
onScreen = new GameScreen;
addChild ( onScreen );
//This sets up the enemy army.
army = new Array(); //sets the "army" as a NEW instance of array.
var newEnemy = new Enemy( 100, -15); //This will create new enemies. There's new var newEnemy statement, hence we call THIS a var.
army.push ( newEnemy ); //the new enemy is added to the army.
addChild( newEnemy ); //the new enemy is added to the game.
//This sets up the player's avatar, a spaceship.
playerShip = new PlayerShip(); //This invokes a new instance of the PlayerShip...
addChild( playerShip ); //...And this adds it to the game.
playerShip.x = mouseX; //These two variables place the "playerShip" on-screen...
playerShip.y = mouseY; //...at the position of the mouse.
///*
//This sets up the player's laser shots:
playerShot = new Array(); //sets the "army" as a NEW instance of array.
var goodShot = new goodLaser( playerShip.x, playerShip.y); //This will create new enemies. There's new var newEnemy statement, hence we call THIS a var.
playerShot.push ( goodShot ); //the new enemy is added to the army.
addChild( goodShot ); //the new enemy is added to the game.
//*/
//This sets up the gameTimer, where a lot of the action takes place.
gameTimer = new Timer( 25 );
gameTimer.addEventListener( TimerEvent.TIMER, onTick );
gameTimer.start();
}
//This function contains the things that happen during the game (player movement, enemy swarms, etc.)
public function onTick( timerEvent:TimerEvent ):void
{
//This "if" statement is where the array that contains the enemy ships is initialized.
if ( Math.random() < 0.05 ) //This sets the number of ships showing up at once.
{
var randomX:Number = Math.random() * 800 //Generates a random number between 0 and 1.
var newEnemy:Enemy = new Enemy ( randomX, -15 ); //This shows where the enemy starts out--at a random position on the X plane, but at a certain points on the Y plane.
army.push( newEnemy ); //This adds the new enemy to the "army" Array.
addChild( newEnemy ); //This makes the new enemy part of the game.
}
//This "for" statement sends the enemies downward on the screen.
for each (var enemy:Enemy in army) //Every time an enemy is added to the "army" array, it's sent downward.
{
enemy.moveDown(); //This is the part that sends the enemy downward.
//And now for the collision part--the part that establishes what happens if the enemy hits the player's spaceship:
if ( playerShip.hitTestObject ( enemy ) ) //If the playerShip makes contact with the enemy...
{
gameTimer.stop(); //This stops the game.
dispatchEvent( new PlayerEvent(PlayerEvent.BOOM) ); //This triggers the game over screen in the PlayerEvent AS
}
}
//This, incidentally, is the player's movement controls:
playerShip.x = mouseX;
playerShip.y = mouseY;
///*
//And this SHOULD be the shooting controls, if the mouse function would WORK...
if ( mouseClick = true )
{
var goodShot = new goodLaser( playerShip.x, playerShip.y); //This will create new lasers. There's new variable in the statement, hence we call THIS a variable.
playerShot.push ( goodShot ); //the new laser is added to the army.
addChild( goodShot ); //the new laser is added to the game.
}
for each (var goodlaser: goodLaser in goodShot)
{
goodlaser.beamGood();
}
//*/
}
}
}
--==--
Sorry if the brackets are coming in uneven, I just wanted to outline the code in its entirety, and show the parts I added where things started going wrong, so someone can tell me what I need to do to make this work.
Basically, everything else works...but when I work on the things connected to the mouse clicking and the array with the lasers, the program stops working. The error seems to be connected to the functions "mouseGoUp" and "mouseGoDown," but I'm not sure how to fix that.
This assignment is due March 8. Can someone help me, please?
Add this import flash.events.MouseEvent; to the top of your class and it should work. You need to import everything you use. That's why you get that error.
As well as importing MouseEvent, in the initialisation of your game, you should add event listeners to the stage which listen for the MOUSE_DOWN and MOUSE_UP events:
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseGoDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseGoUp);
What would be simpler, though, would be to do away with the 'mouseClick' variable and have just a single MouseEvent listener which listens for the MOUSE_DOWN event and trigger your missle launch from the handler.
I had a game like this if you want the entire source i can give it to you but the main parts are
var delayCounter:int = 0;
var mousePressed:Boolean = false;
var delayMax:int = 5;//How rapid the fire is
var playerSpeed:int = 5;
var bulletList:Array = [];
var bullet:Bullet;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true);
stage.addEventListener(Event.ENTER_FRAME,loop,false,0,true);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function loop(e:Event):void
{
player.rotation = Math.atan2(mouseY - player.y,mouseX - player.x) * 180 / Math.PI + 90;
if (bulletList.length > 0)
{
for each (bullet in bulletList)
{
bullet.bulletLoop();
}
}
if (mousePressed)
{
delayCounter++;
if ((delayCounter == delayMax))
{
shootBullet();
delayCounter = 0;
}
}
if (upPressed)
{
player.y -= playerSpeed;
}
if (downPressed)
{
player.y += playerSpeed;
}
if (leftPressed)
{
player.x -= playerSpeed;
}
if (rightPressed)
{
player.x += playerSpeed;
}
}
function mouseDownHandler(e:MouseEvent):void
{
mousePressed = true;
}
function mouseUpHandler(e:MouseEvent):void
{
mousePressed = false;
}
function shootBullet():void
{
var bullet:Bullet = new Bullet(stage,player.x,player.y,player.rotation - 90);
bulletList.push(bullet);
stage.addChildAt(bullet,1);
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
if (event.keyCode == 87)
{
upPressed = true;
}
if (event.keyCode == 83)
{
downPressed = true;
}
if (event.keyCode == 65)
{
leftPressed = true;
}
if (event.keyCode == 68)
{
rightPressed = true;
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
if (event.keyCode == 87)
{
upPressed = false;
}
if (event.keyCode == 83)
{
downPressed = false;
}
if (event.keyCode == 65)
{
leftPressed = false;
}
if (event.keyCode == 68)
{
rightPressed = false;
}
}
BULLET CLASS
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip
{
private var stageRef:Stage;
private var speed:Number = 10;//speed that the bullet will travel at
private var xVel:Number = 5;
private var yVel:Number = 5;
private var rotationInRadians = 0;
public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
this.rotation = rotationInDegrees;
this.rotationInRadians = rotationInDegrees * Math.PI / 180;
}
public function bulletLoop():void
{
xVel = Math.cos(rotationInRadians) * speed;
yVel = Math.sin(rotationInRadians) * speed;
x += xVel;
y += yVel;
if (x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
{
deleteBullet();
}
}
public function deleteBullet()
{
this.visible=false
this.x=9999
this.y=9999
}
}
}

Change Alpha on Rollover; Not Changing Back on Mouse Out?

So I have a Tile class, with a sprite variable that holds the graphic for the tile. On mouse over, I ColorTransform the graphic. Seems to work fine. On mouse out, I try to change it back. Nada. And in fact, rolling over the same tile twice will increase the alpha until eventually it fades totally. Not sure how to fix it. Here's my code.
In the Tile class:
this.addEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);
this.addEventListener(MouseEvent.MOUSE_OUT, thisMouseOut );
public function thisMouseOver( e:Event ):void {
tileGraphic.bitmapData = setAlpha(tileGraphic.bitmapData);
}
public function thisMouseOut( e:Event ):void {
tileGraphic.bitmapData = resetAlpha(tileGraphic.bitmapData);
}
private function setAlpha( bmd:BitmapData ):BitmapData {
var rec:Rectangle = new Rectangle( 0, 0, bmd.width, bmd.height );
var ct:ColorTransform = new ColorTransform();
ct.alphaMultiplier = .65;
bmd.colorTransform( rec, ct );
return bmd;
} //end function setAlpha
private function resetAlpha( bmd:BitmapData ):BitmapData {
var rec:Rectangle = new Rectangle( 0, 0, bmd.width, bmd.height );
var ct:ColorTransform = new ColorTransform();
ct.alphaMultiplier = 1;
bmd.colorTransform( rec, ct );
return bmd;
} //end function resetAlpha
Can anyone point me in the right direction? Thanks!
Replace the resetAlpha with
private function resetAlpha( bmd:BitmapData ):BitmapData {
var rec:Rectangle = new Rectangle( 0, 0, bmd.width, bmd.height );
var ct:ColorTransform = new ColorTransform();
ct.alphaOffset = 255
bmd.colorTransform( rec, ct );
return bmd;
} //end function resetAlpha
You should better change alpha of the container instead of playing with the BitmapData pixels. For example, in your case if your tile bitmap will initially have transparent pixels (fill it with 0x00ff0000 prior to drawing a thing and check if so), they will become opaque with codingbuddha's answer. So, change the listeners to the following:
public function thisMouseOver( e:Event ):void {
tileGraphic.alpha=0.65;
}
public function thisMouseOut( e:Event ):void {
tileGraphic.alpha=1;
}

Actionscript 3, flash: Erasing area dynamically?

long story short, I made image for it.
I am beginner in actionscripting, found out that only animating is not enough.
What I try to accomplish:
You can use a simple mask to achieve this. Here's a working example (using LEFT mouse button, though. I don't think RIGHT one would be suitable for this):
package examples
{
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class MaskTest extends Example
{
private var _toBeErased:Sprite;
private var _discoverable:Sprite;
private var _holesContainer:Sprite;
private var _mouseIsPressed:Boolean = false;
public function MaskTest()
{
}
// Call this method once this Class instance has been added to stage
public function init():void
{
// Create sprites
_toBeErased = new Sprite();
addChild( _toBeErased );
_discoverable = new Sprite();
addChild( _discoverable );
_holesContainer = new Sprite();
addChild( _holesContainer );
// Draw sprites
_toBeErased.graphics.beginFill( 0xFFFF00 );
_toBeErased.graphics.drawRect( 0, 0, 900, 600 );
_toBeErased.graphics.endFill();
_discoverable.graphics.beginFill( 0xFFAAAA );
_discoverable.graphics.drawRect( 0, 0, 900, 600 );
_discoverable.graphics.endFill();
// Set mask
_discoverable.mask = _holesContainer;
// Add mouse listeners
stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
stage.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp );
}
// Mouse listeners callbacks...
private function onMouseDown( e:MouseEvent ):void{
_mouseIsPressed = true;
}
private function onMouseUp( e:MouseEvent ):void{
_mouseIsPressed = false;
}
private function onMouseMove( e:MouseEvent ):void
{
if( !_mouseIsPressed )
return;
// Get a random size for circle
var radius:Number = 5 + (Math.random()*10-5);
// Create new circle and paint it
var circle:Sprite = new Sprite();
circle.graphics.beginFill( 0x000000 );
circle.graphics.drawCircle( 0, 0, radius );
circle.graphics.endFill();
// Move it randomly, just a bit
circle.x = _holesContainer.mouseX + (Math.random()*20-10);
circle.y = _holesContainer.mouseY + (Math.random()*20-10);
_holesContainer.addChild( circle );
}
}
}
Check out http://f6design.com/journal/2009/05/24/erase-an-image-using-your-mouse-in-as3/
Basically, you have to make use of the erase Blend Mode: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BlendMode.html