Flash Actionscript 3 - Simple Btn Click save game/app - actionscript-3

I'm working on a project that requires me to save all of the bits and pieces on the second frame of the stage. This is a glorified dress up game where the user game make design or a piece of art and save the project and come back to it later by clicking the "restore_btn"
This will have multiple 'dragable' bits and pieces on the stage, on the second frame. Could someone give me some insight in how to make it so the app can save on the desktop and when the user opens it up and clicks the 'restore' button their last design loads up on the stage? Thanks for you help. I've had bit of trawl of the net and i can't find any simple tuts for what I need.
Code added, just in case.
p.s please keep it simple as I'm designer. :-)
stop();
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE,follow);
function follow(evt:MouseEvent){
tweezer_cur.x = mouseX;
tweezer_cur.y = mouseY;
}
//Resetter btn ---------------------
reset_btn.addEventListener(MouseEvent.CLICK, startover);
function startover(event:MouseEvent):void
{
gotoAndPlay(1);
}
//------------------------------ fullscreen
function setFullScreen():void {
if (stage.displayState== "normal") {
stage.displayState="fullScreen";
stage.scaleMode = StageScaleMode.NO_SCALE;
} else {
stage.displayState="normal";
}
}
fullbtn.addEventListener(MouseEvent.CLICK, goFull);
// btn declared - - - - - - - -
function goFull(event:MouseEvent):void {
setFullScreen();
};
//---------------------------- print project
//--- all the draggables will live here
dragme.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
dragme.addEventListener(MouseEvent.MOUSE_UP, dropObject);
function pickupObject(event:MouseEvent):void {
event.target.startDrag(true);
}
function dropObject(event:MouseEvent):void {
event.target.stopDrag();
}
//--------
//creating a container as main canvas
var artworkContainers:Sprite = new Sprite();
addChild(artworkContainers);
//example adding content
//var anyContentIWantToPrint:Sprite = new Sprite();
//anyContentIWantToPrint.graphics.beginFill(0, 1);
//anyContentIWantToPrint.graphics.drawRect(0, 0, 1024, 768);
//anyContentIWantToPrint.graphics.endFill();
//artworkContainers.addChild(anyContentIWantToPrint);
printme_btn.addEventListener(MouseEvent.CLICK, startPrintJobHandler, false, 0, true);
function startPrintJobHandler(event:MouseEvent):void
{
var printJob:PrintJob = new PrintJob();
printJob.start()
var printJobOptions:PrintJobOptions = new PrintJobOptions();
printJobOptions.printAsBitmap = true;
//When 'artworkContainer' will be your artwork canvas, where the user will drag and drop. Replace for the instance name you are using.
printJob.addPage(artworkContainers, null, printJobOptions);
printJob.send();
}
// making all of the functions save! --------------------------------
var saveData:SharedObject = SharedObject.getLocal("MyDesign");
if(!saveData.data.test)
saveData.data.test = "Test string";
trace(saveData.data.test); // Test string

This is too long to answer in a comment, so here it goes:
import flash.net.SharedObject;
import flash.geom.Point;
import flash.events.MouseEvent;
var restore_values:SharedObject=SharedObject.getLocal("dress_up_game");
if(!restore_values.data.shirt_point){
restore_values.data.shirt_point=new Point(shirt.x,shirt.y);
restore_values.flush();
}
restore_btn.addEventListener(MouseEvent.CLICK,restore);
function restore(e:MouseEvent){
if(restore_values.data.shirt_point){
shirt.x=restore_values.data.shirt_point.x;
shirt.y=restore_values.data.shirt_point.y;
}
}
shirt.addEventListener(MouseEvent.MOUSE_DOWN,start_shirt_drag);
function start_shirt_drag(e:MouseEvent){
shirt.startDrag()
}
shirt.addEventListener(MouseEvent.MOUSE_UP,stop_shirt_drag);
function stop_shirt_drag(e:MouseEvent){
shirt.stopDrag()
restore_values.data.shirt_point=new Point(shirt.x,shirt.y);
restore_values.flush();
}
It took me a few minutes to scratch this out. Once again, the button's instance name is "restore_btn". The garment or "draggable bit" goes by the instance name "shirt". The code places the shirt at the last saved position if you click the restore button. You should be able to adapt this code to your project's situation on your own.
Cheers,
Drake Swartzy

Yeah, Atriace has the right idea. Check this out:
http://www.republicofcode.com/tutorials/flash/as3sharedobject/
Cheers,
Drake Swartzy

Related

AS3 Fill Color inside line by pen

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.

AS3 Dynamic Text

Im new to ActionScript 3 and am trying to get a number to deplete when i start a timer
So far ive got it so you click the button 'btngo' and a timer begins whilst the ball object moves.
I also want a dynamic text that starts at 100 to deplete down to 0 in 10 seconds when this button is clicked, but unfortunately i have no clue how to do it
Any help will be greatly appreciated, thank you :)
import flash.utils.Timer;
import flash.events.TimerEvent;
stop();
var timer1:Timer = new Timer (10,10000);
var timer2:Timer = new Timer (10,10000);
timer1.addEventListener(TimerEvent.TIMER, forward);
timer2.addEventListener(TimerEvent.TIMER, back);
btngo.addEventListener(MouseEvent.CLICK, green);
btnstop.addEventListener(MouseEvent.CLICK, red);
function green(e:MouseEvent):void {
timer2.stop();
timer1.start();
trace("timer started");
}
function red(e:MouseEvent):void {
timer1.stop();
timer2.start();
trace("timer started");
}
function forward(e:TimerEvent):void {
ball.x += 2;
}
function back(e:TimerEvent):void {
ball.x -= 2;
}
Well, here are two functions which will get you what you are looking for. This first one is the creation of the text:
import flash.text.TextField;
import flash.text.TextFieldType;
var dynamic_text:TextField;
function CreateText()
{
// You can also replace this part with code that creates a textfield that you have in your library,
// in that case just make sure the textfield is set to dynamic in the textfield properties
// Create a new textfield.
dynamic_text = new TextField();
// Make sure its type is set to Dynamic.
dynamic_text.type = TextFieldType.DYNAMIC;
// Position it somewhere on the screen.
dynamic_text.x = 10;
dynamic_text.y = 10;
// Set a default text for now.
dynamic_text.text = "Time: " + time_left;
// Make sure the players cannot select the text.
dynamic_text.selectable = false;
// Add it to your stage (or other parent, take your pick)
stage.addChild(dynamic_text);
}
This second one is called every TimerEvent:
function OnTimerChange()
{
// Update the text based on the new time.
dynamic_text.text = "Time: " + time_left;
}
You will have to call this OnTimerChange function inside your own "forward" and "backward" functions, and in those two functions you will have to calculate how much time has passed.
For an example how to keep track of time that has passed since you pressed a button:
actionscript 3 how to keep track of time elapsed?
Hope this helped.

Sound won't stop when I go back to a previous Stage with code

i have 4 scenes and i put my script for music in scene 2 and then i jump to scene 3 but when i go back to scene 2 and press pause or stop, my music wont do that, but when i play, the music play and i get 2 music start :(, can anybody help the script ?
regards im newbie,
stop();
import flash.events.MouseEvent;
import flash.media.SoundChannel;
//declaring all variables
var isPlaying:Boolean = false;// boolean type of variables can be true or false only
var myMusic = new soothing();// saving music in a varaible
var myChannel:SoundChannel = new SoundChannel();// sound channel Class to stop
var lastPosition:Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
stop_btn.addEventListener(MouseEvent.CLICK, onStopClick);
function onPlayClick(event:MouseEvent):void
{
if (isPlaying == false)
{
isPlaying = true;
myChannel = myMusic.play(lastPosition);
}
myChannel.addEventListener(Event.SOUND_COMPLETE, completeHANDLER);
function completeHANDLER(event:Event):void
{
lastPosition = 0;
isPlaying = false;
}
}
function onPauseClick(event:MouseEvent):void
{
isPlaying = false;
lastPosition = myChannel.position;
myChannel.stop();
}
function onStopClick(event:MouseEvent):void
{
if (isPlaying == true)
{
isPlaying = false;
lastPosition = 0;
myChannel.stop();
}
}
homebtn.addEventListener(MouseEvent.CLICK, qnextScene);
function qnextScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 2");
}
gallerybtn.addEventListener(MouseEvent.CLICK, nxtScene);
function nxtScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 3");
}
mebtn.addEventListener(MouseEvent.CLICK, nextsScene);
function nextsScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 4");
}
Ahhh, yes, the infamous stage sound bug I spent four months chasing down.
One fix: DON'T GO BACKWARDS ON STAGES. They're quite buggy.
Instead, what you put on a given stage, instead put inside a MovieClip. Create two functions instead each of those major MovieClips, one to start all the sounds, animations, etc that need to happen when it appears. The other to STOP sounds, animations, etc.
Then, set up a SINGLE stage with code to swap between these.
One other issue you're going to run into when you start in on this approach is that variables will seemingly clear themselves spontaneously. This is due to the nature of the timeline.These master movieclips should have ONE FRAME ONLY, and stop(); must be the first line of code after the import. If you have multiple frames on these master movieclips, or if you neglect stop, things will keep re-initializing, which is a pain in the butt.
(Heading this off at the pass: yes, code on the timeline is
acceptable, as long as all the code relates directly to the object it
is on the timeline OF. Pure code should be parked in the document
class and linked classes. The idea that you should never put code on
the timeline is a relic practice from AS2.)

Drawing app. How to create undo function in action script 3?

Can anyone show me how can I make undo function? so this is my current action script. I cant figure how to do it and i see some example in some web site, the action script is to long to under stand. Pls show a simple way that i can make this work.
sorry for bad grammar...
import flash.display.MovieClip;
import flash.events.MouseEvent;
var pen_mc:MovieClip;
var drawing:Boolean = false;
var penSize:uint = 1;
var penColor:Number = 0x000000;
function init():void{
pen_mc = new MovieClip();
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
stage.addEventListener(MouseEvent.MOUSE_MOVE, isDrawing);
stage.addEventListener(MouseEvent.MOUSE_UP, finishedDrawing);
addChild(pen_mc);
}
init();
function startDrawing(e:MouseEvent):void{
trace("Pen Has started drawing");
drawing = true;
pen_mc.graphics.lineStyle(penSize, penColor);
pen_mc.graphics.moveTo(mouseX, mouseY);
}
function isDrawing(e:MouseEvent):void{
if(drawing){
pen_mc.graphics.lineTo(mouseX, mouseY);
}
}
function finishedDrawing(e:MouseEvent):void{
trace("finished drawing");
drawing = false;
}
You want to store every line that the user draws in a history array. When the user wants to undo, you can then step backwards through the array and remove each line one by one.

Movieclips clashing with bitmap mask

I am trying to reveal this movie clip image which is originally a bitmap but needs to be used as a bitmap for this purpose. for some reason it's not working ...
It's not throwing any errors... I need this image to be masked as the user presses on it... and later be compared with another bitmap to carry out a function. but for some reason as I mentioned before it's not working out. can somebody please help me?? this is the code for it...
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.BitmapData;
var mouseclick:Number=0;
var maskedbg_mc:maskedbg = new maskedbg ();
var masking:Sprite = new Sprite()
addChild (maskedbg_mc);
maskedbg_mc.x = 18;
maskedbg_mc.y = 343;
var bitmapDataCopy:BitmapData = new BitmapData(742,165,true,0x00FFFFFF);
var b:Bitmap = new Bitmap(bitmapDataCopy);
bitmapDataCopy.draw(maskedbg_mc);
b.mask = masking;
var Testing:BitmapData = new BitmapData(maskedbg_mc.width, maskedbg_mc.height, true, 0x00000000);
addChild(masking);
stage.addEventListener(MouseEvent.MOUSE_DOWN, Pressing);
stage.addEventListener(MouseEvent.MOUSE_MOVE, Moving);
stage.addEventListener(MouseEvent.MOUSE_UP, Lifting);
function Pressing(event:MouseEvent):void {
mouseclick = 1;
}
function Moving(event:MouseEvent):void {
if (mouseclick == 1) {
masking.graphics.beginFill(0x000000);
masking.graphics.drawEllipse(mouseX, mouseY, 70, 60);
masking.graphics.endFill();
}
}
function Lifting(event:MouseEvent):void {
mouseclick = 0;
}
if ( bitmapDataCopy.compare(Testing) ==0 )
{
trace ("Awesomness")
}
Overlooking your code, I notice you are not adding "b" (the masked DisplayObject) to the display list, while you are adding "maskedbg_mc" which actually isn't being masked in your code. Do you have a reason for having these 2 display objects?
I would recommend you following actionscript coding conventions:
http://sourceforge.net/adobe/flexsdk/wiki/Coding%20Conventions/
Your code looks quite confusing when you have both variables and functions with initial letter in uppercase, they look like classes.