Saturday, 23 October 2010

Paper, Scissors and rocks in Actionscript 3 part 1

At some point in your life you have probably played the Paper, Scissors and rocks game using hand gestures. In this tutorial you will learn how to recreate this game using Actionscript 3. I will be covering the basic logic of the game in part 1, and will be including more features in subsequent tutorials. There are three possible ways of winning this game: paper beats rock; scissor beats paper and rock beats scissors. You can also draw as well which is when you both signs are the same.   

Paper, Scissors and rocks in Actionscript 3 part 1

Step 1

Open a new AS3 file. Import images of paper, Scissors and rocks in the library by selecting File >Import > Import to library. You can alternatively use the Shape tools in Flash.

Step 2

Convert the images into movie clips (F8) and give them the following instance names: paper, scissors, and rock accordingly.

Step 3

Select the text tool with dynamic text and drag a text field on the stage. Give the text field the instance name: output_txt.

Step 4

Insert a new layer on the timeline called Actions then open up the Actions panel and enter the following code.

//This adds the mouse click event to the buttons
paper.addEventListener(MouseEvent.CLICK, chosen);
scissor.addEventListener(MouseEvent.CLICK, chosen);
rock.addEventListener(MouseEvent.CLICK, chosen);

//Array to hold the names of the paper, sissor and rock.
var choicesArray:Array = ["paper", "scissor", "rock"]; 

function chosen(e:Event):void{
    //This is holds the player choice.
    var playersChoice:String = String(;
    //This gets a random value from the choicesArray and .
    var computerChoice:String = choicesArray[Math.floor(Math.random()*choicesArray.length)];     

    //player Wins
    if((playersChoice == "paper" && computerChoice == "rock") || 
       (playersChoice == "scissor" && computerChoice == "paper") ||                                                
       (playersChoice == "rock" && computerChoice == "scissor")){
        output_txt.text = "You win! \n The computer chose " + computerChoice;
    //player draw
    else if(playersChoice == computerChoice){
        output_txt.text = "Its a draw \n The computer chose " + computerChoice;
    //player lose
        output_txt.text = "You lose! \n The computer chose " + computerChoice;

Step 5

Test your movie Ctrl + Enter.


Caroline 11 November 2010 at 00:13  

thanks for an awesome tutorial..
I'm quite new to Flash and got lost a bit.
After I typed in your code exactly to the actions fame. I got three errors.

1.function chosen(e:Event):void{
this class or interface "Event" could not be loaded

2.output_txt.text = "You win! \n The computer chose " + computerChoice;    
syntax error

3.output_txt.text = "You win! \n The computer chose " + computerChoice;    
syntax error

could you help me out?



iliketo 14 November 2010 at 03:19  


Try using 'MouseEvent'.

Travis 5 January 2011 at 12:22  

For the second problem, you should put a " after You win!, It's a draw, and You loose!

Mitch 9 February 2011 at 13:26  

pretty awesome!

spnnascarfan 7 March 2011 at 16:29  

Hey! thank you for explaining this but its not working for me. No errors are showing up, but I cant click on any of my objects, the hand isnt even showing up when i move over them, what am I doing wrong?

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP