所以我正在开发这个游戏,现在我在游戏中有两个角色,问题是如果一个角色落后,他就会离开屏幕。当我在屏幕上有一个带有代码行的字符
时,它可以正常工作centerViewportOnGameObject(sonic, 0.0, 0.0, gameEngine.screenWidth/2.0,
gameEngine.screenHeight/2.0);"
但是当我在角色上添加第二个角色时优先,并且不在屏幕上。如何解决这个问题的帮助会很好。谢谢。
以下是游戏玩家的代码:
package tutorials.platformTutorial;
import game.assets.*; import game.engine.*;
import game.physics.*; import game.geometry.*;
public class PlatformLayer extends CollisionSpace {
public static final double GRAVITY_STRENGTH = 800.0;
public PlatformLayer( GameEngine gameEngine ) {
super( "PlatformLayer", gameEngine );
width = 10000;
height = gameEngine.screenHeight;
setGravity(0, GRAVITY_STRENGTH );
addGameObjectCollection( "Platforms" );
addGameObjectCollection( "Balls" );
createBackground();
createPlatforms();
createCharacter();
createCharacter2();
}
private void createBackground() {
GameLayer backgroundLayer =
new GameLayer( "BackgroundLayer", this.gameEngine );
backgroundLayer.width = gameEngine.screenWidth;
backgroundLayer.height = gameEngine.screenHeight;
GameObject background = new GameObject( backgroundLayer );
background.setName("Background");
background.setPosition( backgroundLayer.width/2, backgroundLayer.height/2 );
ImageAssetRibbon backgroundAsset = (ImageAssetRibbon)assetManager.retrieveGraphicalAsset("Background");
background.setRealisation(backgroundAsset);
background.setGeometry( new Box( 0, 0, 1024, 768));
backgroundLayer.addGameObject(background);
gameEngine.addGameLayer(backgroundLayer);
setDrawOrder(backgroundLayer.getDrawOrder() + 1);
}
private void createPlatforms() {
double groundOffset = 0;
while( groundOffset < this.width ) {
createPlatform( groundOffset, this.height - assetManager.retrieveGraphicalAssetArchetype
("Platform").height );
groundOffset += assetManager.retrieveGraphicalAssetArchetype("Platform").width;
}
for( int idx = 0; idx < 8; idx++ )
createPlatform( 300+150 * idx, this.height-50*(idx+1) );
for( int idx = 0; idx < 8; idx++ )
createPlatform( 2500-150 * idx, this.height-50*(idx+1) );
for( int idx = 0; idx < 5; idx++ )
createPlatform( 3500-400 * idx, this.height-50*(idx+1) );
for( int idx = 0; idx < 10; idx++ )
createPlatform( 4500+150 * idx, this.height-50*(idx+1) );
for( int idx = 0; idx < 12; idx++ )
createPlatform( 6000, this.height-50*(idx+1) );
for( int idx = 0; idx < 12; idx++ )
createPlatform( 7200, this.height-50*(idx+1) );
}
private void createPlatform( double x, double y ) {
Body platform = new Body( this );
platform.setRealisationAndGeometry("Platform");
platform.setPosition(x, y);
platform.setMass( Double.MAX_VALUE );
addGameObject( platform, "Platforms" );
}
private void createCharacter() {
SonicSprite sonic = new SonicSprite( this );
sonic.setPosition( 0, gameEngine.screenHeight - sonic.height - 200 );
addGameObject( sonic );
}
private void createCharacter2() {
SonicSprite2 sonic2 = new SonicSprite2( this );
sonic2.setPosition(0, gameEngine.screenHeight - sonic2.height - 200);
addGameObject( sonic2 );
}
@Override
public void update() {
super.update();
updateViewPort();
updateGameObjects();
}
private void updateViewPort() {
GameObject sonic = getGameObject( "Sonic");
GameObject sonic2 = getGameObject( "Sonic2");
centerViewportOnGameObject(sonic, 0.0, 0.0, gameEngine.screenWidth/2.0, gameEngine.screenHeight/2.0);
centerViewportOnGameObject(sonic2, 0.0, 0.0, gameEngine.screenWidth/2.0, gameEngine.screenHeight/2.0);
GameObject background = gameEngine.getGameObjectFromLayer("Background", "BackgroundLayer");
((ImageAssetRibbon)background.getRealisation(0)).setViewPort((int)viewPortLayerX, 0 );
background.getRealisation(0).update();
}
private void updateGameObjects() {
GameObject sonic = getGameObject( "Sonic" );
sonic.update();
GameObject sonic2 = getGameObject( "Sonic2" );
sonic2.update();
GameObjectUtilities.reboundIfGameLayerExited(sonic);
GameObjectUtilities.reboundIfGameLayerExited(sonic2);
}
}