Make a sprite jump when the user clicks on the screen?

I use andengine to implement a sprite that you can drag around the screen with this.

So what I want to do is when the user clicks anywhere on the screen to sprite jump.

or move up and then fall down.

What is the best way to do this with andengine?

+4
source share
2 answers

This should work:

@Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { if(pSceneTouchEvent.isActionDown()) { //Jump only if the user tapped, not moved his finger or something final Entity playerEntity = ...;//Get player entity here. final float jumpDuration = 2; final float startX = playerEntity.getX(); final float jumpHeight = 100; final MoveYModifier moveUpModifier = new MoveYModifier(jumpDuration / 2, startX, startX - jumpHeight); // - since we want the sprite to go up. final MoveYModifier moveDownModifier = new MoveYModifier(jumpDuration / 2, startX + jumpHeight, startX); final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier); playerEntity.registerEntityModifier(modifier); return true; } return false; } 

Keep in mind that if you use this, whenever there is an ACTION_DOWN event, you can skip some other event handlers for this (for example, if you have on-screen buttons, they will never handle the event).

The way to handle it is to use any other objects that you want to receive touch events as touch areas for your scene, Scene.registerTouchArea(ITouchArea) does this. Sprite and AnimatedSprite implement ITouchArea so you can use them.

When a Scene receives a TouchEvent handler, it will first resolve all ITouchArea entries you registered, and then, if they have not been destroyed, it will try to use the onSceneTouchEvenet method.

+3
source

It looks like you need to set up the listener as shown in this lesson: http://www.andengine.org/forums/tutorials/updating-sprites-objects-listeners-t386.html , and then maybe use the move modifier as shown in this post: how to move a sprite object using AndEngine (Android) I hope you find out your problem .;)

Luck-Liyap

+1
source

Source: https://habr.com/ru/post/1390554/


All Articles