2017-05-19 4 views
1

Wie kann ich meinen Player auf dem Bildschirm bleiben lassen, wenn ich ihn mit Beschleunigungssensor und Tastatureingabe nach links und rechts bewege? This image explained my problemWie kann man Spieler auf dem Bildschirm bleiben lassen und geht nicht aus dem Bildschirm in libgdx?

Hier ist mein Code unten

// 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> 

Dankten und Voraus :) Ich bin neu in diesem Rahmen.

+0

Wo ist Ihr Android-Manifest-Datei – Frrank

+0

ich bearbeiten bereits meine Frage Sir @Frrank – jaZzZ

+0

http://stackoverflow.com/questions/27196389/why-does-my-player-move-off-the-screen-when-i- Setzen Sie die-libgdx-Kameras-Position-t. http://stackoverflow.com/questions/18039721/draw-a-sprite-onto-the-map-not-to-the-screen – Frrank

Antwort

0

Below-Codes sieht fadenscheinig:

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

es so sein sollte:

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

Spielobjekte in Libgdx wie Sprite, Image oder andere Drawable mit x-Koordinate an links unten .

+1

Danke, mein Herr! Es klappt :) – jaZzZ

Verwandte Themen