A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Moving MovieClip problem

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    14

    Moving MovieClip problem

    Hello,
    I'm creating a top down perspective shooter where you control a robot with wasd keys. All the graphics like, tiles, enemies and robot himself are placed into a container movieclip. Robot should always remain in the center of the screen when moving; I do that by moving it to the desired direction by some ammount of pixels, and then i move container MC to the opposite dirrection by the same ammount of pixels. The problem is, that whenever i move the robot, it shifts from the centre of the screen by approx .1 pixel, so if you walk long enough, robot from center of the screen, travels to the top of the screen. You can see that by putting the mouse at the tip of the robots gun, then walking for a bit without moving a mouse. The tip of the gun will no longer be pointing to mouse as it was before.
    Heres the code responsible for moving left (code for all other directions is almost the same):
    Code:
    else if(lA)  //left arrow pressed
    {
    	//this is responsible for playing walk animation, not important I suppose
    	if(getTimer() - player.lastStep >= player.animSpeed/player.walkAnim.length)
    	{
    		if(player.legs.currentFrameLabel == "last")
    		{
    			player.legs.gotoAndStop("stand");
    		}
    		else
    		{
    			player.legs.gotoAndStop(player.legs.currentFrame+1);
    		}
    	}
    	player.legs.rotation = 180;
    	playerDirection = "w";
    	
    	//collisions function returns true, if theres no unwalkable object in given coordinates, if true, then robot is moved
    	if(collisions(visTiles, player.x-(timeDiff/player.origAnimSpeed * player.walkSpeed), player.y))
    	{
    		//move player and container
    		player.x -= timeDiff/player.origAnimSpeed * player.walkSpeed;
    		container.x += timeDiff/player.origAnimSpeed * player.walkSpeed;
    	}
    }
    It appears as if player and container are moved by diffirent ammounts of pixels, but whenever i trace those ammounts are absolutely equal. I've tried to put
    Code:
    timeDiff/player.origAnimSpeed * player.walkSpeed
    into a variable and then use that variable to move container and robot, still, nothing changes. I also tried to round up
    Code:
    timeDiff/player.origAnimSpeed * player.walkSpeed
    from real nubmer, to integer and again, nothing changed. Any help on how to keep that guy in a center of the screen would be really appreaciated

    The game can be played here: http://ernyz.lhosting.info/TopDownShooter.html
    I can aswell upload game files, if the need arises.
    Last edited by Ernyz; 01-01-2013 at 10:38 AM.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Try this and make a fixed value before any movement:

    var myMove:Number = timeDiff/player.origAnimSpeed * player.walkSpeed;
    player.x -= myMove;
    container.x += myMove;
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    14
    Thanks for reply . I've tried to do this in the way you said but everything's the same . BTW I've updated my game (link is the same and it's in my first post), so now player x and y coordinates are shown in the hood (those white numbers). Theese coordinates are relevant to stage, not to container mc. When you move, you now can easier see, how robot is shifting up and right by .5-.15 pixel everytime he is moved depending on the direction it goes.

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    You may try something like this:
    var posBefore:Number = player.x;
    player.x -= timeDiff/player.origAnimSpeed * player.walkSpeed;
    var posAfter:Number = player.x;
    container.x = container.x + (posBefore - posAfter);

    I am not sure I am doing the right additions or they have to be subtractions. I once created something similar in AS2. Also you need to do that for the y value as well.

    http://flashscript.biz/actionscript1.../dragtest.html
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    14
    Just tried this and it failed, robot still goes off the centre... Maybe I should try writing to adobe forums?

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Can you post that game fla with the latest code version.
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Junior Member
    Join Date
    Dec 2009
    Posts
    14
    https://www.dropbox.com/sh/rdpcgwp9ax8s201/6UQBYQwxEO
    Movement is in the Main class, function responsible for it starts at line 741. I've modified only movement up, which starts at line 880, everything else remains as it was before I've noticed the problem.

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    First of all I am getting an error, which you need to fix.
    ArgumentError: Error #2109: Frame label stand not found in scene stand.

    I made the changes for moving right and left only as I suggested and it works. You changed up and down but not right and left. The mouse key stays on the robot position now during any up, down, right, left movement.

    http://flashscript.biz/test/FK/Game.zip
    - The right of the People to create Flash movies shall not be infringed. -

  9. #9
    Junior Member
    Join Date
    Dec 2009
    Posts
    14
    Fixed it, though my CS5.5 didn't throw error in that place just needed to delete that line of code.
    Thanks for looking at my code BTW but when i opened .swf which was in archive you sent, the problem remains, despite that I only move straight up/down/right/left. The white numbers which should be 440 and 260 still change while moving (and they shouldn't). Didn't they change when you tested the game?

  10. #10
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    I only placed the mouse over the gun and it stayed at the same position up/down/right/left. I didn't look at any numbers.
    - The right of the People to create Flash movies shall not be infringed. -

  11. #11
    Junior Member
    Join Date
    Dec 2009
    Posts
    14
    Oh, I see. Well, at greater lengths robot still goes off the centre, but it's way better than it was, after I've rewrote movement code as you suggested.
    Thank you for your help, I appreciate it I suppose you can lock, or even better, delete this thread now.

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