LibGDX instanceof 和这个
LibGDX instanceof and this
其实我是 LibGDX 和 Java 的新手,我正在尝试通过观看有关我需要做什么的教程来创建游戏。
所以这是我有特定对象来检查碰撞的问题
public void beginContact(Contact contact)
{
if((contact.getFixtureA().getBody().getUserData() == "player" && contact.getFixtureB().getUserData() instanceof InteractiveTileObjects) )
{
Gdx.app.log("Yeah","");
}
效果很好,
但是当我转到 InteractiveTileObjects 时,代码的最后一部分是 fixture = body.createFixture(fdef);我用它来为特定对象设置用户数据。
这是代码:
bdef.type = BodyDef.BodyType.DynamicBody;
bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MainClass.PPM, (bounds.getY() + bounds.getHeight() / 2) / MainClass.PPM);
body = world.createBody(bdef);
shape.setAsBox((bounds.getWidth() / 2) / MainClass.PPM, (bounds.getHeight()/ 2) / MainClass.PPM);
fdef.shape = shape;
fdef.filter.categoryBits=MainClass.BIT_DCATCHER;
fixture = body.createFixture(fdef);
这是我下面的具体对象之一:
public class DreamCatcher extends InteractiveTileObjects {
public DreamCatcher(World world, TiledMap map, Rectangle bounds)
{
super(world, map, bounds);
fixture.setUserData(this);
setCategoryFilter(MainClass.BIT_DCATCHER);
}
如你所见,我使用
fixture.setUserData(this)
当我将其更改为
fixture.setUserData("DreamCatcher")
由于 InteractiveTileObjects 的实例,它在我的 beginContact 部分不起作用。
但如果我改变
contact.getFixtureB().getUserData() == "DreamCatcher"
它再次完美运行 "this" 是做什么来运行那个 instanceof 代码的?
我的意思是为什么会这样?
我知道它很长,但如果有人能回答这些问题,我会很高兴...
1) instanceof:
在java中instanceof
运算符用于测试对象是否为指定类型的实例(class或subclass或接口)。
java中的instanceof也称为类型比较运算符,因为它将实例与类型进行比较。它 return 是真还是假。如果我们将 instanceof 运算符应用于任何具有空值的变量,它 returns false.
instanceof 运算符示例:
class Animal{}
class Dog1 extends Animal{//Dog inherits Animal
public static void main(String args[]){
Dog1 d=new Dog1();
System.out.println(d instanceof Animal);//true
}
}
2) this 关键词:
java这个关键字可以有很多用法。在java中,这是引用当前对象的引用变量。
java 这个关键词的用法
这里给出java这个关键字的6个用法
- 此关键字可用于引用当前 class 实例变量。
this()
可用于调用当前 class 构造函数。
- 此关键字可用于调用当前 class 方法(隐式)
- 这可以在方法调用中作为参数传递。
- 这可以在构造函数调用中作为参数传递。
- 此关键字也可用于 return 当前 class 实例。
Changing fixture.setUserData(this)
to fixture.setUserData("DreamCatcher")
will not work as setUserData()
method expecting object of type DreamCatcher
and not String.
其实我是 LibGDX 和 Java 的新手,我正在尝试通过观看有关我需要做什么的教程来创建游戏。
所以这是我有特定对象来检查碰撞的问题
public void beginContact(Contact contact)
{
if((contact.getFixtureA().getBody().getUserData() == "player" && contact.getFixtureB().getUserData() instanceof InteractiveTileObjects) )
{
Gdx.app.log("Yeah","");
}
效果很好, 但是当我转到 InteractiveTileObjects 时,代码的最后一部分是 fixture = body.createFixture(fdef);我用它来为特定对象设置用户数据。 这是代码:
bdef.type = BodyDef.BodyType.DynamicBody;
bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MainClass.PPM, (bounds.getY() + bounds.getHeight() / 2) / MainClass.PPM);
body = world.createBody(bdef);
shape.setAsBox((bounds.getWidth() / 2) / MainClass.PPM, (bounds.getHeight()/ 2) / MainClass.PPM);
fdef.shape = shape;
fdef.filter.categoryBits=MainClass.BIT_DCATCHER;
fixture = body.createFixture(fdef);
这是我下面的具体对象之一:
public class DreamCatcher extends InteractiveTileObjects {
public DreamCatcher(World world, TiledMap map, Rectangle bounds)
{
super(world, map, bounds);
fixture.setUserData(this);
setCategoryFilter(MainClass.BIT_DCATCHER);
}
如你所见,我使用
fixture.setUserData(this)
当我将其更改为
fixture.setUserData("DreamCatcher")
由于 InteractiveTileObjects 的实例,它在我的 beginContact 部分不起作用。 但如果我改变
contact.getFixtureB().getUserData() == "DreamCatcher"
它再次完美运行 "this" 是做什么来运行那个 instanceof 代码的? 我的意思是为什么会这样?
我知道它很长,但如果有人能回答这些问题,我会很高兴...
1) instanceof:
在java中instanceof
运算符用于测试对象是否为指定类型的实例(class或subclass或接口)。
java中的instanceof也称为类型比较运算符,因为它将实例与类型进行比较。它 return 是真还是假。如果我们将 instanceof 运算符应用于任何具有空值的变量,它 returns false.
instanceof 运算符示例:
class Animal{}
class Dog1 extends Animal{//Dog inherits Animal
public static void main(String args[]){
Dog1 d=new Dog1();
System.out.println(d instanceof Animal);//true
}
}
2) this 关键词:
java这个关键字可以有很多用法。在java中,这是引用当前对象的引用变量。 java 这个关键词的用法
这里给出java这个关键字的6个用法
- 此关键字可用于引用当前 class 实例变量。
this()
可用于调用当前 class 构造函数。- 此关键字可用于调用当前 class 方法(隐式)
- 这可以在方法调用中作为参数传递。
- 这可以在构造函数调用中作为参数传递。
- 此关键字也可用于 return 当前 class 实例。
Changing
fixture.setUserData(this)
tofixture.setUserData("DreamCatcher")
will not work assetUserData()
method expecting object of typeDreamCatcher
and not String.