2017-05-19 1 views
1

Comment puis-je faire en sorte que mon lecteur reste à l'écran lorsque je le déplace à gauche et à droite à l'aide de l'accéléromètre et du clavier? This image explained my problemComment faire pour que le joueur reste à l'écran et ne sorte pas de l'écran dans libgdx?

Voici mon code ci-dessous

// Load the sprite sheet as a texture 
    cat = new Texture(Gdx.files.internal("catsprite.png")); 
    catsprite = new Sprite(cat); 
    catsprite.setScale(2f); 
    player = new Rectangle(); 
    player.x = Gdx.graphics.getWidth(); 
    player.y = catPlayerY; 

Render

spriteBatch.draw(currentFrame,player.x, player.y); 
    //Keyboard 
    if(Gdx.input.isKeyPressed(Input.Keys.DOWN))player.y -= 250 * Gdx.graphics.getDeltaTime(); 
    if(Gdx.input.isKeyPressed(Input.Keys.UP)) player.y += 250 * Gdx.graphics.getDeltaTime(); 
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) player.x -= 250 * Gdx.graphics.getDeltaTime(); 
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) player.x += 250 * Gdx.graphics.getDeltaTime(); 

    //Mobile acceleration 

    if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) { 
     player.x -= Gdx.input.getAccelerometerX(); 
     player.y += Gdx.input.getAccelerometerY(); 
    } 
    if (player.x < 0) { 
     player.x = 0; 
     player.x += Gdx.graphics.getDeltaTime() *20 *delta; 
    } 
    if (player.x > Gdx.graphics.getHeight()) { 
     player.x = Gdx.graphics.getHeight() ; 
    } 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.hobocat.game" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="25" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/cat_head" 
    android:label="@string/app_name" 
    android:theme="@style/GdxTheme" > 
    <activity 
     android:name="com.hobocat.game.AndroidLauncher" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:configChanges="keyboard|keyboardHidden|orientation|screenSize"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <uses-feature 
     android:name="android.hardware.sensor.accelerometer" 
     android:required="true" /> 
</application> 

</manifest> 

Remercions et avance :) Je suis nouveau dans ce cadre.

+0

Où est votre fichier manifeste android – Frrank

+0

J'ai déjà modifier ma question monsieur @Frrank – jaZzZ

+0

http://stackoverflow.com/questions/27196389/why-does-my-player-move-off-the-screen-when-i- set-the-libgdx-cameras-position-t. http://stackoverflow.com/questions/18039721/draw-a-sprite-onto-the-map-not-to-the-screen – Frrank

Répondre

0

Ci-dessous les codes semble spécieux:

if (player.x > Gdx.graphics.getHeight()) { 
    player.x = Gdx.graphics.getHeight() ; 
} 

Il devrait ressembler à ceci:

if (player.x > Gdx.graphics.getWidth()-player.getWidth()) { 
    player.x = Gdx.graphics.getWidth()-player.getWidth() ; 
} 

objets de jeu dans libgdx comme Sprite, Image ou tout autre objet étirables ayant coordonnée x à gauche bas .

+1

Merci monsieur! Ça marche :) – jaZzZ