A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: URGENT! How to move MC with keyboard within boundaries??

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    16

    URGENT! How to move MC with keyboard within boundaries??

    Hi,

    I want to move simple MC with keyboards in the stage as a boundaries. i have the following code:

    onClipEvent(enterFrame) { if (Key.isDown(Key.UP)) { this._y -= 5; } }
    onClipEvent(enterFrame) { if (Key.isDown(Key.DOWN)) { this._y += 7; } }
    onClipEvent(enterFrame) { if (Key.isDown(Key.LEFT)) { this._x -= 7; } }
    onClipEvent(enterFrame) { if (Key.isDown(Key.RIGHT)) { this._x += 7; } }

    It works.
    Now, how i can make the stage as a boundaries?
    Can i have all the code since i don't know where to add additions exactly.

    Thanks for the help,
    Ben

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hi,

    here you go:

    PHP Code:
    onClipEvent (enterFrame) {
        if (
    Key.isDown(Key.UP)) {
            if(
    this._y 0){
                
    this._y -= 5;
            }
        }
    }

    onClipEvent (enterFrame) {
        if (
    Key.isDown(Key.DOWN)) {
            if(
    this._y Stage.height){
                
    this._y += 7;
            }
        }
    }

    onClipEvent (enterFrame) {
        if (
    Key.isDown(Key.LEFT)) {
            if(
    this._x 0){
                
    this._x -= 7;
            }
        }
    }

    onClipEvent (enterFrame) {
        if (
    Key.isDown(Key.RIGHT)) {
            if(
    this._x Stage.width){
                
    this._x += 7;
            }
        }

    Basically, for example for Key.UP, we check first if the Y Coordinate of your movieclip is GREATER THAN zero, and as long as it is, the movieclip should keep moving up, but once you reach at the end of your Stage, where the Y Coordinate will be zero, it will stop moving the movieclip any further. For Key.DOWN, we check if the Y Coordinate of the movieclip is SMALLER THAN the height of your stage (and the code to get the height of your stage is Stage.height). So, as long as the movieclip's Y Coordinate is smaller than the Y Coordinate of the bottom of your stage, it should keep moving down, but once your movieclip reaches the end, the condition will not be true since it will then NOT be smaller than the coordinate of your Stage's bottom, and it will stop moving downwards. The reason is that when you move your movieclip with the arrow keys, its X and Y coordinates are being changed all the time.

    However, beware that the code is really simple, so that the boundaries will only apply to the CENTER of your movieclip, so if your movieclip is a shape or something, when you reach, let's say, the top of your Stage, it will not stop where you "character" design is, but it will rather stop at the CENTER of your character (or, actually not the center, if you get inside your movieclip, you should see a crosshair at the center of your movieclip, and that is where the movieclip will stop moving when it reaches any of the ends of your Stage). If you want your character to stop when its shape reaches the end, then depending on how your character or movieclip looks like, the difficulty will vary. If the top, right, down and left sides of the character are all equally far away from the center, then the code will be a bit more advanced, but it will still be quite simple-looking, however, if your character has different lengths from the center to the top-, right-, down- and left-side, then more code will be needed or we will have to use not-so-known codes which might be intimidating (but are not).

    Have a good day, and just ask if you need more help
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    16
    Hi,

    It works! this what i looking for.
    Thank you very much for the help and for the all explanations !!

    Have a good day,
    Ben.

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    No problem, I am glad that it was what you were looking for
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

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