dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Scrolling trerrain RPG

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    4

    Scrolling trerrain RPG

    How is it possible to make scrolling terrain that will always keep the player MC in the center, meaning if the player accelerates, it will always show him in the center:

    if the document size is 500x400 and the player's 40x40

    if the player is at _x 700, the player mc will still be in the center.


    Look at this:

    http://www.newgrounds.com/portal/view/408666

    -player is always in the center
    -along with the user interface
    -the player moves because if not the enemy mc will always be next to it




    what ive tried and did not work:

    -vcam, only focused on the player, when I place everything in 1 large MC, the player did not move well and the cam did not follow.
    -making the _root._x equal to the players, but then i turned, everything turns and if i have a custom registration, it messes it all up

    Thanks for reading!

    Edit: As2
    Last edited by katyperry; 03-09-2009 at 05:38 PM.

  2. #2
    Professional Air Guitarist Frag's Avatar
    Join Date
    Dec 2002
    Location
    Brain, Body, Clothes, Computer Chair, Room, House, Neighborhood, City, State, Country, Continent, World, Galaxy, Universe, and on?
    Posts
    811
    Set _root._x/_y to the additive inverse of the player's coordinates and then subtract half of the stage width/height.

    _root._x = -player_x-Stage.width/2;
    _root._y = -player_y-Stage.height/2;

    Haven't used AS2 in a while so the Stage.width might be the incorrect syntax but you get the idea.

  3. #3
    Junior Member
    Join Date
    Feb 2009
    Posts
    4
    It didn't work :<

    I think the Stage.width was for the stage, because I did a trace and it gave me the x of the document.

  4. #4
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Then just fill in the numbers manually, your stages width and height aren't dynamic.

  5. #5
    Senior Member
    Join Date
    Oct 2008
    Posts
    120
    Here is a simple movement (no friction) function with stage focus I use for such games. This will get rid of that jump to the left or right when you first punch down a movement key.
    PHP Code:
    function movement(player:MovieClip,speed:Number,stagX:Number,stagY:Number):Void
    {
        
    _x = -player._x+stagX/2-mov;
        
    _y = -player._y+stagY/2;
        
    player._x += mov;
        if(
    Key.isDown(68)){
            
    mov speed;}
            else if(
    Key.isDown(65)){
                
    mov = -speed;}
                else{
                    
    mov 0;}
    }

    onEnterFrame = function()
    {
        
    movement(MC,NUMBER,StageWidth,StageHeight); //err u understand how to run functions...

    You could also add in a key parameter.
    Last edited by DeadRiver; 03-10-2009 at 08:00 AM.

  6. #6
    Junior Member
    Join Date
    Feb 2009
    Posts
    4
    no luck mate:
    code in frame 1
    Code:
    function movement(player:MovieClip,speed:Number,stagX:Number,stagY:Number):Void
    {
    
        _x = -player._x+stagX/2-mov;
        _y = -player._y+stagY/2;
        player._x += mov;
        if(Key.isDown(68)){
            mov = speed;}
            else if(Key.isDown(65)){
                mov = -speed;}
                else{
                    mov = 0;}
    }
    code in frame 2

    Code:
    stop();
    
    
    onEnterFrame = function()
    {
        movement(player,14,550,400); //err u understand how to run functions...
    }
    no movement on the movieclip with instance name of player, then i also tried linkage name of player

  7. #7
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    place the player mc in the center of the stage. Make a long terrain mc and put on stage( dosent matter if its longer than stage), just make sure terrain mc's registration point is on its left edge, then,
    Code:
    if(left is pressed)
    {
    if((terrainmc._x+4)>=0)
         terrainmc._x=0;
    else
         terrainmc._x+=4;
    }
    That is pseudo code, don't copy paste it!
    When left is pressed, the player dosent move, but the terrain moves towards right, so we are making sure the left edge can move right( >=0 means left side is in stage, else it is outside view) and move it......this is the effect you want.... use similar logic to handle right movement!

  8. #8
    Senior Member
    Join Date
    Oct 2008
    Posts
    120
    code in frame 2?

    If the function isn't with the function call you probably have to do _root.movement();

    Also make sure you use "A" and "D" to move (it can be changed though ofc).

  9. #9
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Quote Originally Posted by DeadRiver View Post
    If the function isn't with the function call you probably have to do _root.movement();
    The function is defined as _root.movement and the call is from _root, so you wouldn't have to.

  10. #10
    Senior Member
    Join Date
    Oct 2008
    Posts
    120
    Oh your right. Just tried. Why isn't it working for him then? That code works perfectly. I use it all the time....
    EDIT:He probably tried to move with the arrows lol =P.

  11. #11
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Nah, the code is just a little worn and torn from all your usage I can't see anything wrong with the code.

    btw, is that a division by two and not a multiplication by a half? Shame, shame.

  12. #12
    Senior Member
    Join Date
    Oct 2008
    Posts
    120
    meh, I always divide by two, it's a curse =(.

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