How can you set the distance that a sprite can move?

  • Greeting Construct 2 users,

    Let's say that you give the bullet behaviour to a sprite and then you want the sprite to stop at a certain distance, you don't want your sprite to travel the entire layout and leave the screen. What kind of code can you enter the Event Sheet?

    Here's the code that I have included in my Event Sheet:

  • If you're using Bullet Behavior it's actually very simple, use the DistanceTravelled expression of your object, then compare it by using system event Compare two values, for example :

    If Bullet.DistanceTravelled = 20

    Bullet : Destroy or Bullet : Set Speed to 0

  • To Tomycase:

    I have included the code that you have suggested but unfortunately, the sprite is not moving and stopping at the desired area. This picture represents how my code was included in my Event Sheet, can you tell me where I have made a mistake so the code cannot work:

  • The following changes should do the trick:

    1. Change "sprEnemyTechnology.Bullet.DistanceTravelled=200" to just "200".

    2. Change the Bullet distance travelled condition from "=" to ">"; it's unlikely that the bullet distance travelled will ever exactly be 200 so if you only check for "=" it probably won't get triggered.

    Also, unless you intend the bullet to stop only if the player points are greater than 30 and it has already travelled 200, move out the "sprEnemyTechnology: Bullet distance travelled" sub-event from under the "Trigger once" event to the same level - that will ensure it stops as soon as it reaches 200.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • To mekonbekon:

    It's still doesn't work. I can see parts of the sprite moving in the upper area of the layout but the sprite is not coming down like is suppose to.

  • Can you detail the exact result you want? By "coming down" what do you mean?

    Also if you can send a capx I can try to find a solution to your problem

  • To Tomycase:

    By coming down I mean I want the sprite located at the top of my layout to move from the top of the layout and then stop at a certain amount of distance at a lower area in my layout. Sorry, I don't want to give my capx file I am not comfortable with this. This is an image of the code that you told me to include in my event sheet, but like I said before it's still doesn't work, so I don't know if you have another idea:

  • Okay so I recreated a similar situation, the object "Enemy" angle is adjusted to 90° as soon as player points reach or exceed 30, then bullet behavior is activated with speed=200 and travelled distance will be checked if it reachs 100 px or more :

    The blue bar height = 100 and the red marker follows the enemy Y position, that way you can visualise the trajectory progress : i.ibb.co/wptVt5S/GIF.gif

    However it can sometime be innacurate by a few pixels, that's why I added the last action to adjust Enemy Y at the very end, so it is repositioned immediatly at the good spot;

    Hope that helped you ~

  • To Tomycase:

    Good news, the code works! Thank you very much for your help!

  • No problem :)

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