dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [F8] object movement

  1. #1
    Junior Member
    Join Date
    Sep 2006
    Posts
    10

    [F8] object movement

    I am trying to make the user move an object around the screen with the arrow keys. I created a movie clip in which I created a ball, then dragged the ball onto the main stage, given it an instance name of "ball", and added the following actionscript to Frame 1 of the main timeline.

    code:
    	if(Key.isDown(Key.LEFT)) {
    ball._x -= 5;
    }
    if(Key.isDown(Key.RIGHT)) {
    ball._x += 5;
    }
    if(Key.isDown(Key.UP)) {
    ball._y += 5;
    }
    if(Key.isDown(Key.DOWN)) {
    ball._y -=5;
    }



    What I told it to do is move the ball 5 pixels to whatever direction the user wants. It is supposed to work, except that it doesn't. What am I doing wrong?
    Attached Files Attached Files

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    your code is activated only one time
    you need to loop to continuously poll the keyboard -
    Code:
    this.onEnterFrame = function(){
    with(ball){
    if(Key.isDown(Key.LEFT)) { _x -= 5; }
    if(Key.isDown(Key.RIGHT)) { _x += 5; }
    if(Key.isDown(Key.UP)) { _y += 5; }
    if(Key.isDown(Key.DOWN)) { _y -=5; }
    }
    };

  3. #3
    Junior Member
    Join Date
    Sep 2006
    Posts
    10
    Thanks. I tried many examples already posted on this board, but they don't seem to work.

    With the code that I already posted earlier, why does it only activate once?

  4. #4
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    you say that the code is on frame#1.
    Flash will read through that code, and either move on to other code on the frame, or move onto the next frame.
    You need to continually run the code to watch for keypresses so you add an onEnterFrame event around the code block. Adding the event will now run the movement code at the Frames Per Second setting of your movie.

    hth

  5. #5
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Removing any helpful previous posts I mistakenly contributed to these forums.

    See ya FK'ers
    Last edited by NTD; 10-19-2006 at 06:10 AM.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center