本文整理汇总了Java中com.badlogic.gdx.physics.box2d.Body.setUserData方法的典型用法代码示例。如果您正苦于以下问题:Java Body.setUserData方法的具体用法?Java Body.setUserData怎么用?Java Body.setUserData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.badlogic.gdx.physics.box2d.Body
的用法示例。
在下文中一共展示了Body.setUserData方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createStaticBoxBody
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticBoxBody(float x, float y, float width, float height) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y from map
y = Gdx.graphics.getHeight() - y;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
PolygonShape polygon = new PolygonShape();
((PolygonShape) polygon).setAsBox(width * WORLD_TO_BOX / 2, height
* WORLD_TO_BOX / 2);
groundBody.createFixture(polygon, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例2: createStaticCircleBody
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticCircleBody(float x, float y, float radius) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y of object from map
y = Gdx.graphics.getHeight() - y;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
CircleShape shape = new CircleShape();
((CircleShape) shape).setRadius(radius * WORLD_TO_BOX / 2);
groundBody.createFixture(shape, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例3: createStaticBoxBody
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticBoxBody(float x, float y, float width, float height) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y from map
y = tileMapRenderer.getMapHeightUnits() - y;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
PolygonShape polygon = new PolygonShape();
((PolygonShape) polygon).setAsBox(width * WORLD_TO_BOX / 2, height
* WORLD_TO_BOX / 2);
groundBody.createFixture(polygon, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例4: createStaticCircleBody
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticCircleBody(float x, float y, float radius) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y of object from map
y = tileMapRenderer.getMapHeightUnits() - y;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
CircleShape shape = new CircleShape();
((CircleShape) shape).setRadius(radius * WORLD_TO_BOX / 2);
groundBody.createFixture(shape, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例5: createEntityBody
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
/**
* Erlaubt es den Unterklassen möglichst einfach einen beliebigen Box2D Körper zu erstellen.
*
* @param position die Startposition des Body
* @param shape die Form, die für dne Body verwendet werden soll
* @param type der Typ des Körpers
* @return ein Box2D Körper
*/
protected Body createEntityBody(Vector2 position, Shape shape, BodyDef.BodyType type)
{
position.scl(Physics.MPP);
BodyDef bodyDef = new BodyDef();
bodyDef.type = type;
bodyDef.position.set(position);
bodyDef.fixedRotation = true;
Body body = worldObjectManager.getPhysicalWorld().createBody(bodyDef);
body.setUserData(this);
FixtureDef fixture = new FixtureDef();
fixture.shape = shape;
fixture.filter.categoryBits = Physics.CATEGORY_ENTITIES;
fixture.filter.maskBits = Physics.MASK_ENTITIES;
body.createFixture(fixture).setUserData(this);
shape.dispose();
return body;
}
示例6: update
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
@Override
public void update(float gameTime) {
// implement the world physics
world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
world.clearForces();
Iterator<Body> bi = world.getBodies();
while (bi.hasNext()) {
Body b = bi.next();
if (b == null)
continue;
if (b.getUserData() instanceof IPhysicsObject) {
IPhysicsObject e = (IPhysicsObject) b.getUserData();
// get the x, y position
float x = b.getPosition().x * BOX_TO_WORLD - e.getRegionWidth()
/ 2;
float y = b.getPosition().y * BOX_TO_WORLD
- e.getRegionHeight() / 2;
// set position and rotation of sprite based the body in world
// physics
e.setX(x);
e.setY(y);
e.setRotation(MathUtils.radiansToDegrees * b.getAngle());
e.update(gameTime);
} else if (b.getUserData() instanceof String) {
String str = (String) b.getUserData();
if ("remove".equals(str)) {
b.setUserData(null);
world.destroyBody(b);
b = null;
}
}
}
}
示例7: createStaticBody
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
private Body createStaticBody(TiledObject o) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// get x from tile object
float x = o.x;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y of object from map
float y = tileMapRenderer.getMapHeightUnits() - o.y - o.height;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
// get the polygon point from map
String[] strp = o.polygon.split(" ");
Vector2[] apoints = new Vector2[strp.length];
// for each points from objects
for (int i = 0; i < strp.length; i++) {
x = Float.parseFloat(strp[i].split(",")[0]); // get x
x = x * WORLD_TO_BOX; // transform into box2d
y = -Float.parseFloat(strp[i].split(",")[1]); // get y
y = y * WORLD_TO_BOX; // transform into box2d
// insert the point into the array
apoints[i] = new Vector2(x, y);
}
// create static body from point array
groundBox.set(apoints);
groundBody.createFixture(groundBox, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例8: createFireBall
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
public void createFireBall() {
//创建刚体
Body ballBody = createFireBallBody(mWorld);
//设置火球出现位置
FireBall ball;
if (STATE == State.STATE_RIGHT_FIREBALL) {
ball = mBallController.createFireBall(ballBody, State.STATE_RIGHT_FIREBALL);
} else {
ball = mBallController.createFireBall(ballBody, State.STATE_LEFT_FIREBALL);
}
ballBody.setUserData(ball);
//将火球添加到火球管理类中并给火球速度赋值
mBallController.addFireBalls(ball);
}
示例9: createStaticBody
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticBody(TiledObject o) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// get x from tile object
float x = o.x;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y of object from map
TileRenderer renderer = getGameService()
.getService(TileRenderer.class);
float y = renderer.getMapHeightUnits() - o.y - o.height;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
// get the polygon point from map
String[] strp = o.polygon.split(" ");
Vector2[] apoints = new Vector2[strp.length];
// for each points from objects
for (int i = 0; i < strp.length; i++) {
x = Float.parseFloat(strp[i].split(",")[0]); // get x
x = x * WORLD_TO_BOX; // transform into box2d
y = -Float.parseFloat(strp[i].split(",")[1]); // get y
y = y * WORLD_TO_BOX; // transform into box2d
// insert the point into the array
apoints[i] = new Vector2(x, y);
}
// create static body from point array
groundBox.set(apoints);
groundBody.createFixture(groundBox, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例10: createBodyAt
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
public Body createBodyAt(Vector2 pt, AtlasRegion frame)
{
// Get the sprite from the sprite sheet
candy = new Image(frame);
candy.setOrigin(candy.getWidth()/2, candy.getHeight()/2);
this.addActor(candy);
// Defines the body of your candy
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(new Vector2(pt.x/PTM_RATIO, pt.y/PTM_RATIO));
bodyDef.linearDamping = 0.3f;
Body body = world.createBody(bodyDef);
body.setUserData(candy);
// Define the fixture as a polygon
FixtureDef fixtureDef = new FixtureDef();
PolygonShape spriteShape = new PolygonShape();
Vector2[] verts = {
new Vector2((-candy.getWidth()/2) / PTM_RATIO,
(-candy.getHeight()/2) / PTM_RATIO),
new Vector2((candy.getWidth()/2) / PTM_RATIO,
(-candy.getHeight()/2) / PTM_RATIO),
new Vector2((candy.getWidth()/2) / PTM_RATIO,
(candy.getHeight()/2) / PTM_RATIO),
new Vector2((-candy.getWidth()/2) / PTM_RATIO,
(candy.getHeight()/2) / PTM_RATIO),
};
spriteShape.set(verts);
fixtureDef.shape = spriteShape;
fixtureDef.density = 30.0f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.9f;
fixtureDef.filter.categoryBits = 0x01;
fixtureDef.filter.maskBits = 0x01;
body.createFixture(fixtureDef);
bodies.add(body);
return body;
}
示例11: createEnemy
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
private void createEnemy() {
mEnemyDaos = new Array<Enemy>();
Array<MapLayer> mapLayers = new Array<MapLayer>();
//天兵对象层
mapLayers.add(mMap.getLayers().get("enemy"));
mapLayers.add(mMap.getLayers().get("enemyFu"));
mapLayers.add(mMap.getLayers().get("enemyQiang"));
for (MapLayer mapLayer : mapLayers) {
if (mapLayer == null) {
continue;
}
//初始化天兵刚体形状
BodyDef enemyDef = new BodyDef();
enemyDef.type = BodyDef.BodyType.DynamicBody;
//多边形形状
PolygonShape polygonShape = new PolygonShape();
//设置夹具
FixtureDef enemyFixDef = new FixtureDef();
//遍历enemy对象层
for (MapObject object : mapLayer.getObjects()) {
//坐标
float x = 0;
float y = 0;
//获取对象坐标
if (object instanceof EllipseMapObject) {
EllipseMapObject ellipseMapObject = (EllipseMapObject) object;
x = ellipseMapObject.getEllipse().x / Constant.RATE;
y = ellipseMapObject.getEllipse().y / Constant.RATE;
}
//天兵夹具
polygonShape.setAsBox(30 / Constant.RATE, 60 / Constant.RATE);
enemyFixDef.shape = polygonShape;
enemyFixDef.isSensor = true;
enemyFixDef.filter.categoryBits = Constant.ENEMY_DAO;
enemyFixDef.filter.maskBits = Constant.PLAYER;
//设置位置
enemyDef.position.set(x, y);
Body enemyBody = mWorld.createBody(enemyDef);
enemyBody.createFixture(enemyFixDef).setUserData("enemy");
//创建脚传感器 foot
polygonShape.setAsBox(28 / Constant.RATE, 3 / Constant.RATE, new Vector2(0, -58 / Constant.RATE), 0);
enemyFixDef.shape = polygonShape;
enemyFixDef.filter.categoryBits = Constant.ENEMY_DAO;
enemyFixDef.filter.maskBits = Constant.BLOCK;
enemyFixDef.isSensor = false;
enemyBody.createFixture(enemyFixDef).setUserData("enemyFoot");
Enemy enemy = new Enemy(enemyBody, mapLayer.getName());
mEnemyDaos.add(enemy);
enemyBody.setUserData(enemy);
Thread thread = new Thread(enemy);
thread.start();
}
}
}
示例12: createBoss
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
private void createBoss(int level) {
MapLayer mapLayer = mMap.getLayers().get("Boss");
if (mapLayer == null) return;
//初始化Boss刚体形状
BodyDef bossDef = new BodyDef();
bossDef.type = BodyDef.BodyType.DynamicBody;
//多边形形状
PolygonShape polygonShape = new PolygonShape();
//设置夹具
FixtureDef bossFixDef = new FixtureDef();
//遍历Boss对象层
for (MapObject object : mapLayer.getObjects()) {
//坐标
float x = 0;
float y = 0;
//获取对象坐标
if (object instanceof EllipseMapObject) {
EllipseMapObject ellipseMapObject = (EllipseMapObject) object;
x = ellipseMapObject.getEllipse().x / Constant.RATE;
y = ellipseMapObject.getEllipse().y / Constant.RATE;
}
//设置位置
bossDef.position.set(x, y);
Body bossBody = mWorld.createBody(bossDef);
//二郎神标志
if (Play.level == 4) {
polygonShape.setAsBox(20 / Constant.RATE, 10 / Constant.RATE, new Vector2(0, -20 / Constant.RATE), 0);
bossFixDef.shape = polygonShape;
bossFixDef.isSensor = true;
bossBody.createFixture(bossFixDef).setUserData("erlang");
}
//boss夹具
polygonShape.setAsBox(30 / Constant.RATE, 60 / Constant.RATE);
bossFixDef.shape = polygonShape;
bossFixDef.isSensor = true;
bossFixDef.filter.categoryBits = Constant.BOSS;
bossFixDef.filter.maskBits = Constant.PLAYER;
bossBody.createFixture(bossFixDef).setUserData("boss");
//创建脚传感器 foot
polygonShape.setAsBox(28 / Constant.RATE, 3 / Constant.RATE, new Vector2(0, -58 / Constant.RATE), 0);
bossFixDef.shape = polygonShape;
bossFixDef.filter.categoryBits = Constant.BOSS;
bossFixDef.filter.maskBits = Constant.BLOCK;
bossFixDef.isSensor = false;
bossBody.createFixture(bossFixDef).setUserData("bossFoot");
//设置Boss
switch (level) {
case 0: //第一关
mBoss = new Boss(bossBody, MyGdxGame.assetManager.getTextureAtlas(Constant.BOSS_JULING_ROLE));
break;
case 1: //第二关
mBoss = new Boss(bossBody, MyGdxGame.assetManager.getTextureAtlas(Constant.BOSS_ZENGZHANG_ROLE));
break;
case 2: //第三关
mBoss = new Boss(bossBody, MyGdxGame.assetManager.getTextureAtlas(Constant.BOSS_GUANGMU_ROLE));
break;
case 3: //第四关
mBoss = new Boss(bossBody, MyGdxGame.assetManager.getTextureAtlas(Constant.BOSS_DUOWEN_ROLE));
break;
case 4: //第五关
mBoss = new Boss(bossBody, MyGdxGame.assetManager.getTextureAtlas(Constant.BOSS_ERLANG_ROLE));
break;
}
bossBody.setUserData(mBoss);
Thread thread = new Thread(mBoss);
thread.start();
}
}
示例13: initStick
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
private void initStick() {
mStickLeafTexture = new Texture(Gdx.files.internal("stick_leaf.png"));
mStickHandleTexture = new Texture(Gdx.files.internal("stick_handle.png"));
final float handleWidth = 20.f;
final float handleHeight = mWorldHeight * 0.4f; // 40% of screen height
mStickHandleDimensions = new Rectangle(
(mWorldWidth / 2.f) - (handleWidth / 2.f),
0,
handleWidth,
handleHeight
);
final float leafWidth = (mWorldWidth / 3.f) * 2.f; // 2/3 of screen width
final float leafHeight = 20.f;
mStickLeafDimen = new Rectangle(
(mWorldWidth / 2) - (leafWidth / 2),
handleHeight,
leafWidth,
leafHeight
);
// create physics body
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(
mStickLeafDimen.x + (mStickLeafDimen.width / 2),
mStickLeafDimen.y + (mStickLeafDimen.height / 2));
// Create a polygon shape
PolygonShape leafPhysicsBox = new PolygonShape();
leafPhysicsBox.setAsBox(mStickLeafDimen.width / 2, mStickLeafDimen.height /2);
Body body = mWorld.createBody(bodyDef);
body.setUserData(new BodyUserDate(BODY_USER_DATA_TYPE_OTHER));
Fixture fixture = body.createFixture(leafPhysicsBox, 0.f);
fixture.setFriction(0.4f);
fixture.setRestitution(0.4f);
leafPhysicsBox.dispose();
}
示例14: initPoints
import com.badlogic.gdx.physics.box2d.Body; //导入方法依赖的package包/类
private void initPoints() {
float pointOffsetX = 22.f;
float pointOffsetY = 32.f;
float radius = 12.f;
mPointsTextureRegion = new TextureRegion(
new Texture(Gdx.files.internal("element_yellow_polygon_glossy.png")));
mPointLeft = new Circle(mStickLeafDimen.x - pointOffsetX,
mStickLeafDimen.y + mStickLeafDimen.height + pointOffsetY,
radius);
// create physics body
BodyDef bodyDefL = new BodyDef();
bodyDefL.type = BodyDef.BodyType.StaticBody;
bodyDefL.position.set(mPointLeft.x, mPointLeft.y);
CircleShape pointShapeL = new CircleShape();
pointShapeL.setRadius(mPointLeft.radius);
Body mPointLeftBody = mWorld.createBody(bodyDefL);
mPointLeftBody.setUserData(new BodyUserDate(BODY_USER_DATA_TYPE_POINT_LEFT));
Fixture fixtureLeftPoint = mPointLeftBody.createFixture(pointShapeL, 0.f);
fixtureLeftPoint.setSensor(true);
pointShapeL.dispose();
// Right points
mPointRight = new Circle(mStickLeafDimen.x + mStickLeafDimen.width + pointOffsetX,
mStickLeafDimen.y + mStickLeafDimen.height + pointOffsetY,
radius);
// create physics body
BodyDef bodyDefR = new BodyDef();
bodyDefR.type = BodyDef.BodyType.StaticBody;
bodyDefR.position.set(mPointRight.x, mPointRight.y);
CircleShape pointShapeR = new CircleShape();
pointShapeR.setRadius(mPointRight.radius);
Body mPointRightBody = mWorld.createBody(bodyDefR);
mPointRightBody.setUserData(new BodyUserDate(BODY_USER_DATA_TYPE_POINT_RIGHT));
Fixture fixtureRightPoint = mPointRightBody.createFixture(pointShapeR, 0.f);
fixtureRightPoint.setSensor(true);
pointShapeR.dispose();
}