How do I move a sprite half way across the screen only?

0 favourites
  • 8 posts
From the Asset Store
This is a code so you can see the same skin on all screens (peers). Each peer gets a skin, in addition to 4 directions t
  • I'm trying to make a 2 player Pong type game but rather than having just up/down direcions..I have 4 directions.....my problem is trying to find a way to control the distance of the left/right movement. I don't want my players to move any than the center of the screen whether the player is on the left or the right side of screen.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • if player1 X coordinates > than window width/2 then set player1 X to window width/2

    same goes for player2 if X coordinates < window width/2 then set player2 x window width/2

  • Hope this helps .. Easiest way i came up with .... Im sure there is a more precise way to do it but that just came off the top of my head and was real simple

  • Oh, was too late - same idea, but I also have a second solutions so I post it anyway

    I'm not sure if I understand you correctly

    Do you mean you don't want player1 to cross to the other side?

    If yes, I have two solutions

    You can make an invisible border with a sprite block that can't be crossed. Or make a custom key movement for your player and give him a second condition (the following example subtracts half of the player x size - in this case 16 if the origin point is centered)

    If pressed right

    and

    Player position.x <= windowwidth/2 - 16

  • thank you both for your replies...ill try them out.. I did try putting an invisible sprite in the center of the screen but I gave it a solid behaviour and that made the players stop but the Ball bounced off of it and just stayed on 1 half of screen

  • I would do something like this:

    system every tick

    • player1 set x = clamp(player.x,viewportleft(0), viewportleft(0)+0.5*(viewportright(0)-viewportleft(0))
    • player2 set x = clamp(player.x, viewportright()-0.5*(viewportright(0)-viewportleft(0)), viewportright(0))
  • thanks LittleStain, that's done the trick

  • fr0z3n, your suggestion worked for my player 1 , it moved bout 3/4 way across its side of the screen but for player 2 , it moved past the center of screen and stopped equal with player 1s furthest point.

    thanks again everyone for taking the time and your help

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)