Create a scratch card in Actionscript 3
In this tutorial you will learn how to create a scratch card effect in Actionscript 3. This effect will allow you to click and drag the mouse to reveal the image below, like a real scratch card. This tutorial uses masks to create the scratch card effect. You will also need an image to be revealed. I have used a free stock image, but you can use whatever image you wish.
In AS2 the scratch card effect would have been quite difficult, as there were no shape classes to create the masked shape. You would have to manually write lots of code to create simple shapes like a circle. This is because there were only the lineTo() and curveTo() functions which meant you would have to use complicated maths to create any advanced shapes. However, in AS3 there are new shapes classes which reduce the code you need to write, and makes the process of the creating the scratch card easier.
Scratch card in Actionscript 3
Step 1
Open a new Flash AS3 file.
Set an appropriate stage size I have selected 400 x 200 dimensions, but you can use whatever size you wish.
Select the rectangle tool (R) with any colour and drag a rectangle shape to fill the entire stage area.
Now select the text tool (T) and type the message “Have a scratch” on top of your rectangle shape. I have used impact font type with #666666 colour.

Step 2
On the timeline lock “layer 1” and insert a new layer.
Then import your image on the stage by selecting File > Import > Import to Stage. Make sure your image fits exactly on top of your rectangle. You can use the free transform tool (Q) to adjust the image size.

Step 3
Select your image on the stage and convert it into a symbol by pressing F8. Give it an appropriate name, check movie clip and click ok. Now select your movie clip and give it the instance name: maskedbg_mc.

Step 4
On the timeline insert a new layer called “Actions”. Right click on the first frame and select Actions and enter the following code:
//1.
var mouseclick:Number=0;
//2.
var mask_mc:Sprite = new Sprite();
maskedbg_mc.mask = mask_mc;
addChild(mask_mc);
//3.
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseD);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseM);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseU);
//4.
function mouseD(event:MouseEvent):void {
mouseclick = 1;
}
//5.
function mouseM(event:MouseEvent):void {
if (mouseclick == 1) {
mask_mc.graphics.beginFill(0x000000);
mask_mc.graphics.drawEllipse(mouseX, mouseY, 70, 60);
mask_mc.graphics.endFill();
}
}
//6.
function mouseU(event:MouseEvent):void {
mouseclick = 0;
}
Code:
1. Creates a variable called “mouseclick” and sets the number value to zero.
2. Creates a new sprite called “mask_mc” and sets the masked objects to the background image. Also adds the sprite onto the stage.
3. Adds three event listeners for the mouse down, move and up events. Each of the three event listeners has different functions.
4. This function gives the “mouseclick” variable a value of 1, when the mouse is depressed.
5. This function checks whether the mouse has been depressed. If the mouse has been depressed, then any mouse movements will revealed the image below. The mask_mc is an ellipse shape with the position at the mouse x and y location; the width = 70 and height = 60. (Checkout the Actionscript 3.0 graphics component reference for more details)
6. This function returns the “mouseclick” variable to 0, which stops the image being revealed below.
Step 5
Test your scratch card Ctrl + Enter. Now use your mouse to click and drag, you should see the image below.
You should now have a scratch card effect. The source files can be downloaded here.
Checkout part 2 where you can have a random background.



39 comments:
Doesn't work for me - throws out the error: "1120: Access of undefined property maskedbg_mc.
@Peter
This error message means you have not given the instance name 'maskedbg_mc'. Make sure you have done Step 3.
I tried this today and had a box over it with a line of code inside. The scratch card worked but had the writing over the top. Any advice on how to stop this?
@emmy
I don't understand what you mean. Can you explain more?
I think emmy means the code behind a box of color were both on the stage and the layer with the code was above the layer with the box.
@emmy
make sure whatever is going to be seen when the cover is scratched off is on a layer beneath the scratch off layer.
Is there a way to put a link on the revealed layer? Can the scratch off work without having to click down, thus saving the click for a link?
@Justin
You basically need to modify the code by removing the click event listener and the function.
I have entered all coding correctly, no errors, and the scratch card effect isn't working. help?
@Gardengnomes
Are you clicking and dragging?
Yup, I'm clicking.
@Gardengnomes
Since, you have no coding errors. You must have missed out something from the tutorial. In step 2, have you inserted a new layer on the timeline.
I have the three layers required. still nothing.
@Gardengnomes
It probably something very simple. Have you converted the image into a movie clip (F8) and given it the instance name maskedbg_mc.
Oh, haha, I forgot to add the second part of the coding. :) Thanks anyways, it's good to see that you respond asap. Kudos! :D
Have a nice day.
It doesnt work with me.. I have done everything you said in the tutorial, (i did it twice) but it doesn't work. I have exactly the same image, but it doesn't scratch :-(. Please help me!
thanks
@Ossscar
You need to be more specific, are there any error codes? It is not a difficult tutorial it has only 4 steps.
Hey there =]
Very cool tut, Helped me alot !
I've done it exactly but got a little problem here, when i try to test the movie i get this error:
**Error** Scene=Scene 1, layer=Actions, frame=1:Line 5: The class or interface 'Sprite' could not be loaded.
var mask_mc:Sprite = new Sprite();
**Error** Scene=Scene 1, layer=Actions, frame=1:Line 15: The class or interface 'MouseEvent' could not be loaded.
function mouseD(event:MouseEvent):void {
**Error** Scene=Scene 1, layer=Actions, frame=1:Line 20: The class or interface 'MouseEvent' could not be loaded.
function mouseM(event:MouseEvent):void {
**Error** Scene=Scene 1, layer=Actions, frame=1:Line 29: The class or interface 'MouseEvent' could not be loaded.
function mouseU(event:MouseEvent):void {
Total ActionScript Errors: 4 Reported Errors: 4
Is that my AS version ? I'm using flash 8 !
thanks,
Mostafa Berg
@Mostafa Berg
This tutorial is created in Actionscript 3.0. It looks like you are using Actionscript 2.0. This effect will only work in Actionscript 3.0, so it is giving you errors.
I just noticed that !:(
Too bad i can't use this technique, but good news i'm already working on a different one and it's working great for me in AS2 =]
Thanks alot !, and thanks for your great quick response :)
Mostafa
Nice trick, but how would I go about to check weather of not everything was scratched correctly?
don't worry :) solved it myself
Best wishes
Hey there iliketo,
great tutorial! it helped me a lot!
I just wanted to ask, in case u ca help me, is there any way to understand if a sprite has the same color in all its surface?
My intention is after all img is scratched to make a video play-button appear!
Any idea how to do that?
thnx in advance
Ize
nice! done it and works fine... its a start for me in flash. Thanks for this tutorial
Thanks for this great tutorial ... I tested it and it's working like a charm .. but got a little problem ..
when I try to embed the exported swf file in a HTML page the scratch effect just doesn't work! .. I set stage.scaleMode = "noScale"
but still, it's not working when it's embedded into HTML page..
BTW I'm using external image loaded by load function but as I mentioned before it's working properly in flash player but it doesn't work when it's embedded in HTML page..
Thanks alot!
Great Tutorials, they're such a great help! But with this one, when i test it, all thet comes up is the image, rather than the scratch card! I'm pretty sure i've done everything as I was meant to, I was wondering if you could help! thanks :)
Hi there,
Thanks for the tutorial. It is great.
I am just wondering if it's possible that randomly appears a winner (let say one out of ten).
Can we do that in AS3?
Hi this is really helpful, are you able to add a scratching sound to this to make it seem real?
Hi,
I just complete the tutorial and it's turned great. But I thought that I could add a reset button, I tryed:
RESET.addEventListener(MouseEvent.CLICK, FILL);
function FILL(event:MouseEvent):void
{
trace("click");
mask_mc.graphics.beginFill(0xFFFFFF);
mask_mc.graphics.endFill();
}
But doesn't work... Someone can help me? Thanks ;)
@Flippa
What do you want the reset button to do?
hi
can it scratch without mouse click?
how can i customize so i dont need to mouse click to start scratching
can i scrath just by moving the mouse?
@Jennifer
Remove the mouse down event listener and remove the code with 'mouse click'
hi iliketo
is it possible to dynamically calculate the visible ratio of the bg image ?
thanks dima
Is there a way to have it so that every time you scratch, it is a different image each time?
@link_man96
I've created part 2 of this tutorial. You can see the link at the end of this tutorial.
keep up the good work
help us from coding!!:)
@Dima
I believe you can use the Bitmap.historgram().
Well it works greats ! Many thanks
Small improve that can turn it more realistic : use dropshadow filter on your image sprite, and set it rather thin and check inside/inner box..
And voilà !
You reveal what seems to be under :)
I’m new to as 3 ad I have the mask revealing working. But how do I check if the picture is 80% revealed?
@Jonne
I believe you can use the Bitmap.historgram().
Post a Comment