我有以下问题: 我有课
public class HockeyActivity extends SimpleBaseGameActivity implements IOnSceneTouchListener, IOnAreaTouchListener {
并在此类声明中:
final Player player= new Player( PhysicsFactory.createCircleBody( this.mPhysicsWorld, player.face, BodyType.DynamicBody,CIRCLE_FIXTURE_DEF));
这里java大叫无法找到player.face
现在在HockeyActivity类中声明了Player类:
class Player {
final Body facebody;
final Sprite face = new Sprite(HockeyActivity.CAMERA_WIDTH / 2 + 200f,
240, HockeyActivity.mFaceTextureRegion,
getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_MOVE:
// Here 'body' refers to the Body object associated with
// this sprite
facebody.setTransform(pSceneTouchEvent.getX(),
pSceneTouchEvent.getY(), facebody.getAngle());
break;
default:
break;
}
return true;
}
};
Player(Body f) {
facebody = f;
}
}
如何处理而不改变最终的事情?
我也不能静态,因为在HockeyActivity类中声明了getVertexBufferObjectManager()
:
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
我处理问题的另一种方法:
final Sprite face = new Sprite(CAMERA_WIDTH/2+200f, 240, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
switch(pSceneTouchEvent.getAction()){
case TouchEvent.ACTION_MOVE:
//Here 'body' refers to the Body object associated with this sprite
facebody.setTransform(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), facebody.getAngle());
break;
default:
break;
}
return true;
}
//几行之后:
final Body facebody = PhysicsFactory.createCircleBody( this.mPhysicsWorld, face, BodyType.KinematicBody,CIRCLE_FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,facebody, true, true));
facebody.setUserData("player1");
但问题出在final Sprite face
内,当覆盖方法时,因为它没有看到facebody
声明。如何使这个Overriding方法看到facebody?
答案 0 :(得分:0)
你的代码有些不对 你想要实例化一个属性“播放器”,但使用/传递属性本身作为参数之一 这是不可能的(至少在java中)。
你可以做什么,如果“face”是一个常数,是:
像这样:
final Player player = new Player( PhysicsFactory.createCircleBody( this.mPhysicsWorld, Player.FACE, BodyType.DynamicBody,CIRCLE_FIXTURE_DEF));
否则,不要在实例化时使用参数传递player.face,而是在初始化播放器对象后尝试使用setter。