+ Reply to Thread
Results 1 to 5 of 5

Thread: Flash: Keyboard and Mouse Movement

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Flash: Keyboard and Mouse Movement

    Keyboard Movement

    My last tutorial was on Graphic Primitives. Now we are going to look at how to make graphics move. I'm also going to show you how to draw stuff on the stage, convert it to a symbol and use ActionScript code to make it do something.

    Open up Flash and go to Window to create a new document. We need to show the toolbox so you have the tools you need to draw something. Press Ctrl-F2 to make the toolbox appear. If you have used paint then most of this will be visible to you. I like to draw faces so I am going to draw a smiley face.

    You can draw whatever you want. At this point you need to use these tools to draw something on the stage. My drawing is shown below:



    Now, press V and you will have the selection tool selected. You will now select everything that is part of your drawing. Then you are going to right click on it and go down to convert to symbol. In the bottom corner is a button that says "advanced". You need to click on that.



    The two things you need to do is change the name of the symbol and check Export for ActionScript.I am going to call mine Smiley. You can leave th4e class name the same. Just note what it is. You need to know the class name for your ActionScript code. In the popup click Okay.

    You now have the Smiley set up for use in ActionScript code. You now can clear your stage. The rest of what we are going to do is in the action script code. Go to frame1 in layer1 and press F9. This will open the Actions panel.

    Adding a Symbol via AS

    First we need to create an instance of the class. Then we will set a location and add it to the stage.

    Code:
    var smiley:Smiley = new Smiley();
    That is all you need to create a new Smiley. Now we can set a location and add it to the stage.

    Code:
    smiley.x = 20;
    smiley.y = 20;
    
    stage.addChild(smiley);
    First we set the smiley to appear at (20,20) and then we add it to the stage. Note, you do not have to include stage you could just use addChild but I find it makes more sense to say what we are adding the smiley to.

    When you run the movie now, you should see a smiley on the stage.

    Adding an Event Listener

    We need an event listener, to listen for a key press. We will add the event listener to the stage so when the user presses a key the smilye will move.

    Add this code:

    Code:
    stage.addEventListener(KeyboardEvent.KEY_DOWN,move);
    When a key is pressed the move function is called.

    Here is the move function:

    Code:
    var nKey:int = e.keyCode;
    	
    	switch (nKey) {
    		case Keyboard.LEFT:
    			smiley.x -= 5;
    			break;
    		case Keyboard.RIGHT:
    			smiley.x += 5;
    			break;
    		case Keyboard.UP:
    			smiley.y -= 5;
    			break;
    		case Keyboard.DOWN:
    			smiley.y += 5;
    			break;
    	}
    This code takes the key code (an integer that represents which key was pressed) and checks it against the four directions. We then change the x and y coordinates to make the smiley move.

    This isn't really difficult and it makes sense when you read it. However, this concept is what you would use in a game of some sorts.

    Mouse Movement

    We are going to do one more thing and that is mouse movement. When the user moves the mouse we are going to show the mouse coordinates on the screen.

    Add a label to the stage, call it lblMouse. Using the properties window make the width 200 and in the components inspector (remove all the text). The label should appear empty. Now, we need to add an event listener to the stage to listen for mouse movements.

    Code:
    stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
    Whenever the mouse moves over the stage (basically forever) we call the mouseMove function.

    This simply updates the label to show the coordinates of the mouse.

    Code:
    function mouseMove(e:MouseEvent):void {
    	lblMouse.text = "X: " + String(mouseX) + " Y: " + String(mouseY);
    }
    That briefly covers the concepts in getting user input with the keyboard and mouse. These are crucial for games. Thankfully, Flash and ActionScript make it so easy.

    You can see the file product here.
    Attached Thumbnails Attached Thumbnails Flash: Keyboard and Mouse Movement-guy.jpg   Flash: Keyboard and Mouse Movement-symbol.jpg  
    Last edited by chili5; 08-27-2009 at 09:54 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Flash: Keyboard and Mouse Movement

    Well done, again
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Flash: Keyboard and Mouse Movement

    Thanks. If you want to see what it looks like, I posted a SWF file.

  5. #4
    Jordan Guest

    Re: Flash: Keyboard and Mouse Movement

    I like these tutorials! Why not post the SWF on your website?
    +rep

  6. #5
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Flash: Keyboard and Mouse Movement


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Mouse Movement
    By ryansithe in forum C and C++
    Replies: 1
    Last Post: 06-09-2010, 09:38 PM
  2. simulating mouse movement in java
    By ViZon in forum Java Help
    Replies: 2
    Last Post: 03-27-2010, 05:24 PM
  3. simulating mouse clicks and keyboard events
    By ViZon in forum Java Help
    Replies: 1
    Last Post: 10-14-2009, 10:01 PM
  4. KVM (switch for keyboard, monitor and mouse)
    By jwxie518 in forum Technology Ramble
    Replies: 0
    Last Post: 06-22-2009, 07:00 PM
  5. [C++] Keyboard/Mouse emulation
    By Muted in forum Classes and Code Snippets
    Replies: 3
    Last Post: 06-17-2009, 11:45 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts