ActionScript 3.0: Moving to another scene by clicking a button - actionscript-3

I have created the first scene of my small project. Now I want to move to the second scene of the app. The start one is called startScene, the second one playScene. Here is the code of the class linked to the first scene:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;
public class dragMain extends MovieClip {
public function dragMain() {
setWelcomeMessage();
drawArrow();
createStartButton();
}
// funzione per settare il messaggio di benvenuto
function setWelcomeMessage(): void {
// TextField contenete il messaggio di benvenuto
var welcomeMessage: TextField = new TextField();
// formattazione per il messaggio di benvenuto
var welcomeFormat: TextFormat = new TextFormat("Verdana", 40, 0xFF0000);
// imposto il testo da visualizzare
welcomeMessage.text = "Welcome, click the button to start playing";
//
welcomeMessage.autoSize = TextFieldAutoSize.LEFT;
// cerco di centrare il testo ad occhio nella schermata
welcomeMessage.x = 500;
// applico la formattazione al testo
welcomeMessage.setTextFormat(welcomeFormat);
// aggiungo il testo allo stage
addChild(welcomeMessage);
}
// funzione che disegnera' la freccia
function drawArrow(): void {
// Sprite che conterra' la freccia disegnata
var arrow: Sprite = new Sprite();
arrow.graphics.beginFill(0XFF0000);
arrow.graphics.moveTo(800, 100); //500,500 // 200,200
arrow.graphics.lineTo(1000, 100); //700,500 // 400,200
arrow.graphics.lineTo(1000, 550); //700,950 // 400,650
arrow.graphics.lineTo(1100, 550); //800,950 // 500,650
arrow.graphics.lineTo(900, 700); //600,1100// 300,800
arrow.graphics.lineTo(700, 550); //400,950 // 100,650
arrow.graphics.lineTo(800, 550); //500,950 // 200,650
arrow.graphics.lineTo(800, 100); //500,500 // 200,200
addChild(arrow);
}
// funzione per creare il bottone
function createStartButton(): void {
var button: Sprite = new Sprite();
button.graphics.beginFill(0xFF0000);
button.graphics.moveTo(700, 800);
button.graphics.lineTo(1100, 800);
button.graphics.lineTo(1100, 1000);
button.graphics.lineTo(700, 1000);
button.graphics.lineTo(700, 800);
var clickMeMessage: TextField = new TextField();
clickMeMessage.x = 855;
clickMeMessage.y = 865;
var welcomeFormat: TextFormat = new TextFormat("Verdana", 40, 0x000000);
// imposto il testo da visualizzare
clickMeMessage.text = "click me!";
//
clickMeMessage.autoSize = TextFieldAutoSize.CENTER;
// applico la formattazione al testo
clickMeMessage.setTextFormat(welcomeFormat);
addChild(button);
addChild(clickMeMessage);
button.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(evt: MouseEvent): void {
gotoAndPlay(1, "playWindow");
}
}
}
When I click the button that I created through code I'm always on the startScene and I can't move to the playScene. I have got a frame on startScene and a frame on playScene. What's the problem and how can I solve it? Thank you!

First things first.
You have done gotoAndPlay so it will keep iterating between all the scenes on frame 1
gotoAndStop(1,"playScene");
If you don't want all the stuffs you have added on start scene you should remove them or set their visibility to false because addchild adds the object to the stage
This is what i did.I made the following variables global
var welcomeMessage: TextField;
var button: Sprite;
var clickMeMessage: TextField;
var arrow: Sprite;
and changed the onclick function a bit
function onClick(evt: MouseEvent): void {
button.visible = false;
clickMeMessage.visible = false;
arrow.visible = false;
welcomeMessage.visible = false;
gotoAndStop(1, "playScene");
}
well i think this should work

Try gotoAndStop(1, "playWindow"); instead of gotoAndPlay(). I suspect you don't have a stop() on frame 1 of your playScene so Flash is continuing past that scene.

Related

Error: Access of undefined property in AS3

I'm attempting to learn ActionScript 3 as my first programming language (before this I only did in past some little crap with PHP).
I have this code:
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
/**
* ...
* #author Mattia Del Franco
*/
[Frame(factoryClass="Preloader")]
public class Main extends Sprite
{
[Embed(source = "img/pgnew.png")]
internal var MyImage:Class;
// La riga embed importa l'immagine, la riga sotto la assegna ad una classe chiamata MyImage
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);
// entry point
trace ("Hello World!");
var myBitmap:Bitmap = new MyImage; //nuova variabile myBitmap al quale viene assegnato la creazione di un nuovo MyImage (trattato come un oggetto)
addChild( myBitmap );
var writeText:TextField = new TextField();
writeText.text = "Ciao Mondo!";
this.addEventListener(MouseEvent.CLICK, function(){
addChild(writeText);
var clicked:Boolean = true;
return clicked;
});
this.addEventListener(MouseEvent.CLICK, function() {
if (clicked == true) {
removeChild(writeText);
} else {
addChild(writeText);
}
});
}
}
}
In the second EventListener I'm trying to get the boolean value of clicked (specified in the first EventListener) but when i go to debug this program i get this error:
col: 9 Error: Access of undefined property clicked.
if (clicked == true) {
Why this happens?
The reason that you cannot access the "clicked" variable is because this variable is held in a different scope. When you declare a variable within a function (your first Event Listener), it is only accessible from within that function. Your second Event Listener has no access to that variable.
Here is a good way to work around the problem:
var clicked:Boolean = false;
var writeText:TextField = new TextField();
writeText.text = "Ciao Mondo!";
this.addEventListener(MouseEvent.CLICK, function(){
addChild(writeText);
clicked = true;
return clicked;
});
this.addEventListener(MouseEvent.CLICK, function() {
if (clicked == true) {
removeChild(writeText);
} else {
addChild(writeText);
}
});

as3 - dispatch mouse event from external class

I am having problems understanding correctly how to dispatch events and capture them in another class.
In this case, I am trying to emulate a mouse click dispatched from an "clickM" class.
On stage I have 2 movieclips to test, a custom cursor and the listeners to capture the click event.
clickM:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event; //dispatcher
import flash.events.MouseEvent;// mouse event
public class clickM extends MovieClip {
private var delay: uint = 3000;
private var repeat: uint = 0; //se va por todo el tiempo
private var myTimer: Timer = new Timer(delay, repeat);
public function clickM() {
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}
private function timerHandler(e: TimerEvent): void {
//repeat--;
//statusTextField.text = ((delay * repeat) / 1000) + " seconds left.";
trace ( "simulate click...");
//dispatchEvent(new MouseEvent(MouseEvent.CLICK));
this.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
}
}
}
Stage code, rojo & morado are movieclips:
import flash.events.MouseEvent;
stage.addEventListener(Event.ENTER_FRAME, myFunction);
var mano: clickM = new clickM();
mano.name = "mano";
addChild (mano);
morado.addEventListener(MouseEvent.CLICK, cl);
rojo.addEventListener(MouseEvent.CLICK, cl);
stage.addEventListener(MouseEvent.CLICK, cl);
function myFunction(event: Event) {
mano.x = mouseX;
mano.y = mouseY;
}
function cl(e: MouseEvent) {
trace("click over " + e.target.name);
}
If I click over morado or rojo, there's no problem - I can get their names. If I just let the code run, I can't get their names, I just get "mano", which is the custom cursor I'm using.
How can I get the desired behavior?
Regards.
Add mouseEnabled=false; inside clickM constructor. This should make Flash to ignore your mano in the event dispatch phase, so the underlying object should be the primary target if there's any, otherwise the target will be the stage. If your custom cursor contains more movie clips, you should also add mouseChildren=false;.
public function clickM() {
mouseEnabled=false;
// possibly add this too
mouseChildren=false;
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}

As3 collision test

Im doing a space invaders game in flash when i came to collisions i had a 1061 error possibly undefined hittestobject through a reference with a static type class....
How can i fix that? tried many ways can't get rid of that error
/* Código que pára a timeline na 1 frame para que o menu continue apresentado*/
stop();
/*Movimenta a nave fazendo a seguir os movimentos do rato e esconde o cursor do sistema operacional*/
stage.addChild(arma_tiro);
arma_tiro.mouseEnabled = false;
arma_tiro.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
arma_tiro.x = stage.mouseX;
}
Mouse.hide();
/* Mouse Click Event
Clicking on the specified symbol instance executes a function in which you can add your own custom code.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the symbol instance is clicked.
*/
stage.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
var bullet:bullet_ = new bullet_();
addChild(bullet);
bullet.x=arma_tiro.x;
bullet.y=arma_tiro.y;
bullet.addEventListener(Event.ENTER_FRAME, moverbala);
}
function moverbala(event:Event):void // função para mover a bala para cima */
{
event.target.y=event.target.y-20;
}
//stage.addEventListener(Event.ENTER_FRAME, Primeira);
setInterval(Primeira, 1000) ; //define intervalo de tempo entre as varias repetiçoes da funçao
function Primeira(){ //funçao de spawn de nave 1
var invader1:invader_1 = new invader_1();
addChild(invader1);
invader1.x=0;
invader1.y=15;
invader1.addEventListener(Event.ENTER_FRAME, mover1);
}
function mover1(event:Event):void // função para mover a nave para lado direito */
{
event.target.x+=10;
}
//Nave 2
setInterval(Segunda, 1000) ; //define intervalo de tempo entre as varias repetiçoes da funçao
function Segunda(){ //funçao de spawn de nave 1
var invader2:invader_2 = new invader_2();
addChild(invader2);
invader2.x=0;
invader2.y=45;
invader2.addEventListener(Event.ENTER_FRAME, mover2);
}
function mover2(event:Event):void // função para mover a nave para lado direito */
{
event.target.x+=10;
}
//Nave 3
setInterval(Terceira, 1000) ; //define intervalo de tempo entre as varias repetiçoes da funçao
function Terceira(){ //funçao de spawn de nave 1
var invader3:invader_3 = new invader_3();
addChild(invader3);
invader3.x=0;
invader3.y=85;
invader3.addEventListener(Event.ENTER_FRAME, mover3);
}
function mover3(event:Event):void // função para mover a nave para lado direito */
{
event.target.x+=10;
}
// error line
if (bullet_.hitTestObject(invader_1))
{
//Remove bullet and enemy
mcGameStage.removeChild(bullet_);
mcGameStage.removeChild(invader_1);
}
it seems that bullet_ is a class and not an instance so you cannon call hitTestObject on it.
maybe try replace bullet_ by bullet.
there are many solution to do that but the simplest for me would be to keep 2 array, one for the bullets, one for the enemies.
so add arrays:
// create the array for the bullets
bullets :Array = [];
// create the array for the enemies
enemies :Array = [];
add an onEnterFrame event listener to do the tests and the game logic on each frame:
addEventListener( Event.ENTER_FRAME, _gameLoop );
change your function to create bullets and enemies:
function fl_MouseClickHandler_3( event:MouseEvent ):void
{
// create the bullet
var bullet:bullet_ = new bullet_();
addChild(bullet);
bullet.x=arma_tiro.x;
bullet.y=arma_tiro.y;
// add the bullet to the bullets array
bullets.push( bullet );
}
function Primeira():void
{
var invader1:invader_1 = new invader_1();
addChild(invader1);
invader1.x=0;
invader1.y=15;
enemies.push( invader1 );
}
function Segunda():void
{
var invader2:invader_2 = new invader_2();
addChild(invader2);
invader2.x=0;
invader2.y=45;
enemies.push( invader2 );
}
function Terceira():void
{
var invader3:invader_3 = new invader_3();
addChild(invader3);
invader3.x=0;
invader3.y=85;
enemies.push( invader3 );
}
now create the game loop function:
function _gameLoop():void
{
var firstLoop:Boolean = true;
// loop to move/remove the bullets
for( var a:int = bullets.length-1; a>=0; a-- )
{
bullets[a].y -= 20;
// if the bullet is not on screen anymore, remove it from array
if( bullets[j].y < 0 )
{
removeChild( bullet[a] );
bullets.splice(a,1);
continue;
}
}
// loop enemies
for( var i:int = enemies.length-1; i>=0; i-- )
{
// move the enemy
enemies[i].x += 10;
// loop the bullets to see if on collide the enemy
for( var j:int = bullets.length-1; j>=0; j-- )
{
// test collision with the enemy
if( enemies[i].hitTestObject( bullets[j] )
{
// make your enemy dead
removeChild( enemies[i] );
// remove it from the array
enemies.splice(i,1);
}
}
}
}
Hope this could helps you

error 1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip

Building a top down shooter, and after fixing some bugs an making everything work i am left with only one more.
error 1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip.
This is the Level.as where all the object are loaded
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.utils.Timer;
import flash.text.TextField;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.utils.*;
import flash.display.Shape;
import flash.display.DisplayObject
/**
*
*/
public class Level extends Sprite
{
//these booleans will check which keys are down
public var leftIsPressed:Boolean = false;
public var rightIsPressed:Boolean = false;
public var upIsPressed:Boolean = false;
public var downIsPressed:Boolean = false;
//how fast the character will be able to go
public var speed:Number = 5;
public var vx:Number = 0;
public var vy:Number = 0;
//how much time before allowed to shoot again
public var cTime:int = 0;
//the time it has to reach in order to be allowed to shoot (in frames)
public var cLimit:int = 12;
//whether or not the user is allowed to shoot
public var shootAllow:Boolean = true;
//how much time before another enemy is made
public var enemyTime:int = 0;
//how much time needed to make an enemy
//it should be more than the shooting rate
//or else killing all of the enemies would
//be impossible :O
public var enemyLimit:int = 16;
//the spaceAuto's score
public var score:int = 0;
//this movieclip will hold all of the bullets
public var bulletContainer:MovieClip = new MovieClip();
//whether or not the game is over
public var gameOver:Boolean = false;
public var SpaceAuto:spaceAuto = new spaceAuto;
// publiek toegangkelijke verwijzing naar deze class
public static var instance:Level;
public var particleContainer:MovieClip = new MovieClip();
// constructor code
public function Level()
{
instance = this;
SpaceAuto.x = 135;
SpaceAuto.y = 350;
addChild(this.SpaceAuto);
addChild(this.bulletContainer);
Project.instance.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
Project.instance.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
Project.instance.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
Project.instance.stage.addEventListener(Event.ENTER_FRAME, generateParticles);
//checking if there already is another particlecontainer there
if(!contains(particleContainer))
{
//this movieclip will hold all of the particles
addChild(this.particleContainer);
}
}
function keyDownHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case Keyboard.LEFT : leftIsPressed = true; break;
case Keyboard.RIGHT : rightIsPressed = true; break;
case Keyboard.UP : upIsPressed = true; break;
case Keyboard.DOWN : downIsPressed = true; break;
}
if(e.keyCode == 32 && shootAllow)
{
//making it so the user can't shoot for a bit
shootAllow = false;
//declaring a variable to be a new Bullet
var newBullet:Bullet = new Bullet();
//changing the bullet's coordinates
newBullet.x = SpaceAuto.x + SpaceAuto.width/2 - newBullet.width/2;
newBullet.y = SpaceAuto.y;
//then we add the bullet to stage
bulletContainer.addChild(newBullet);
}
}
function keyUpHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case Keyboard.LEFT : leftIsPressed = false; break;
case Keyboard.RIGHT : rightIsPressed = false; break;
case Keyboard.UP : upIsPressed = false; break;
case Keyboard.DOWN : downIsPressed = false; break;
}
}
function enterFrameHandler(e:Event):void {
vx = -int(leftIsPressed)*speed + int(rightIsPressed)*speed;
vy = -int(upIsPressed)*speed + int(downIsPressed)*speed;
SpaceAuto.x += vx;
SpaceAuto.y += vy;
if(cTime < cLimit)
{
cTime ++;
trace("++")
}
else
{
//if it has, then allow the user to shoot
shootAllow = true;
//and reset cTime
cTime = 0;
}
//adding enemies to stage
if(enemyTime < enemyLimit)
{
//if time hasn't reached the limit, then just increment
enemyTime ++;
}
else
{
//defining a variable which will hold the new enemy
var newEnemy = new Enemy();
//making the enemy offstage when it is created
newEnemy.y = -1 * newEnemy.height;
//making the enemy's x coordinates random
//the "int" function will act the same as Math.floor but a bit faster
newEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));
//then add the enemy to stage
addChild(newEnemy);
//and reset the enemyTime
enemyTime = 0;
}
//updating the score text
txtScore.text = 'Score: '+score;
if (gameOver == true)
{
loadScreenThree();
}
}
function generateParticles(event:Event):void
{
//so we don't do it every frame, we'll do it randomly
if(Math.random()*10 < 2){
//creating a new shape
var mcParticle:Shape = new Shape();
//making random dimensions (only ranges from 1-5 px)
var dimensions:int = int(Math.random()*5)+1;
//add color to the shape
mcParticle.graphics.beginFill(0x999999/*The color for shape*/,1/*The alpha for the shape*/);
//turning the shape into a square
mcParticle.graphics.drawRect(dimensions,dimensions,dimensions,dimensions);
//change the coordinates of the particle
mcParticle.x = int(Math.random()*stage.stageWidth);
mcParticle.y = -10;
//adding the particle to stage
particleContainer.addChild(mcParticle);
}
//making all of the particles move down stage
for(var i:int=particleContainer.numChildren - 1; i>=0; i--){
//getting a certain particle
var theParticle:DisplayObject = particleContainer.getChildAt(i);
//it'll go half the speed of the character
theParticle.y += speed*.5;
//checking if the particle is offstage
if(theParticle.y >= 400){
//remove it
particleContainer.removeChild(theParticle);
}
}
}
/**
* eventhandler voor als je op de tweede knop klikt
*/
private function loadScreenThree():void
{
// eerst opruimen!
cleanListeners();
// dan naar ander scherm
Project.instance.switchScreen( "derde" );
}
private function cleanListeners():void
{
stage.removeEventListener(Event.ENTER_FRAME, generateParticles);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
}}
Where it goes wrong is in the Enemy Class in the eFrame Function. Here a loop runs trough the array bulletContainer from Level.as. A new variable is made and it should consist of the bullet that is in the loop out of the bulletContainer, and here comes the error in place
package{
//we have to import certain display objects and events
import Level;
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Sprite;
//this just means that Enemy will act like a MovieClip
public class Enemy extends MovieClip
{
//VARIABLES
//this will act as the root of the document
//so we can easily reference it within the class
private var _root:Object;
//how quickly the enemy will move
private var speed:int = 5;
//this function will run every time the Bullet is added
//to the stage
public function Enemy()
{
//adding events to this class
//functions that will run only when the MC is added
addEventListener(Event.ADDED, beginClass);
//functions that will run on enter frame
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function beginClass(event:Event):void
{
_root = Sprite(root);
}
private function eFrame(event:Event):void
{
//moving the bullet up screen
y += speed;
//making the bullet be removed if it goes off stage
if(this.y > 400)
{
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
}
//checking if it is touching any bullets
//we will have to run a for loop because there will be multiple bullets
for(var i:int = 0;i<Level.instance.bulletContainer.numChildren;i++)
{
//numChildren is just the amount of movieclips within
//the bulletContainer.
trace("LOOOOOOOOOP");
trace(Level.instance.bulletContainer.getChildAt(i));
//we define a variable that will be the bullet that we are currently
//hit testing.
var bulletTarget:Sprite = Level.instance.bulletContainer.getChildAt(i);
//now we hit test
if(hitTestObject(bulletTarget))
{
//remove this from the stage if it touches a bullet
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
//also remove the bullet and its listeners
Level.instance.bulletContainer.removeChild(bulletTarget);
//bulletTarget.removeListeners();
//up the score
Level.instance.score += 5;
}
}
//hit testing with the user
if(hitTestObject(Level.instance.SpaceAuto))
{
//losing the game
trace("DOOOOOD");
removeListeners();
this.parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, eFrame);
Level.instance.gameOver = true;
}
}
public function removeListeners():void
{
this.removeEventListener(Event.ENTER_FRAME, eFrame);
}
}}
i have tried make it Sprite or anything else but i just dont know how to fix this so it wil take the bullet so i van see if it hits the Enemy
var bulletTarget:Sprite = Level.instance.bulletContainer.getChildAt(i);
getChildAt will return a DisplayObject type, and hitTestObject requires a parameter with DisplayObject type.
So try this in your Enemy's eFrame function.
var bulletTarget:DisplayObject = Level.instance.bulletContainer.getChildAt(i);

How can I make a color picker, choosing three differents colors?

I want to be able to choose three differents colors from a color picker. I want to choose between red, green and blue. How can I change my code below to do this? Should I have my color picker in its own class?
public class SuperDraw extends Sprite
{
private var loadImage:LoadImage;
private var saveImage:SaveImage;
private var graphic:GameGraphic;
private var bmp:Bitmap;
private var pencilDraw:Shape;
private var p:Point;
//private var backGroundGame:backGround;
private var backGroundDraw:drawTabel;
private static var penSprite:Sprite;
private static var mouseDownFlag:Boolean;
protected var penSprite:Sprite = new Sprite();
// Bitmap där ritandet sparas.
protected var canvasBitmap:Bitmap;
public function SuperDraw()
{
graphic = new GameGraphic();
loadImage = new LoadImage();
saveImage = new SaveImage();
backGroundDraw = new drawTabel();
//-------------------------------------------
// Skapa en backgrund där ritandet ska visas.
this.canvasBitmap = new Bitmap( new BitmapData( this.stage.stageWidth, this.stage.stageHeight ), "auto", true );
// Adda alla klasser och ha graphic klassen ovanför penSprite.
this.addChild(graphic);
this.addChild(backGroundDraw);
this.addChild(penSprite);
this.addChild(saveImage);
this.addChild(loadImage);
this.penSprite.graphics.lineStyle(3, 0x000000 );
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown);
}
private function mouseDown(e:MouseEvent):void
{
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
this.stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseUp);
}
private function mouseMove(e:MouseEvent):void
{
penSprite.graphics.lineTo( e.localX, e.localY );
}
// Skapar funktionen mouseUp.
private function mouseUp(e:MouseEvent):void
{
// Ritar och sparar till bitmapData.
canvasBitmap.bitmapData.draw( penSprite, null, null, null, null, true );
// Ta bort alla listeners.
this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
this.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
}
}
maybe you want this
import fl.controls.ColorPicker;
var cp:ColorPicker = new ColorPicker();
cp.colors = [ 0xff0000,0x00ff00,0x0000ff];
addChild(cp);
for more info,you can visit http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/ColorPicker.html