好吧,这应该很简单,但我在数学上把自己给绊倒了。使用AndEngine BTW>
我正在使用一些教程……画面左边的英雄(风景)向右射击。一切都运行得很好。现在我想让英雄在屏幕的右边拍摄左边。我在原地打转,希望能得到一些帮助。这是我用来做左英雄,右射击的代码。
/** shoots a projectile from the player's position along the touched area */
private void shootProjectile(final float pX, final float pY) {
int offX = (int) (pX - (hero.getX()));
int offY = (int) (pY - (hero.getY() + hero.getHeight()/2));
if (offX <= 0) return;
// position the projectile on the player and set up path
projectile = pPool.obtainPoolItem();
int realX = (int) (mCamera.getWidth() - (hero.getX() ) );
float ratio = (float) realX / (float) offX;
int realY = (int) ((offY * ratio));
float length = (float) Math.sqrt((realX * realX) + (realY * realY));
float velocity = 280.0f / .5f; // 480 pixels per (sec)f on screen
float realMoveDuration = length / velocity;
// defining a moveBymodifier from the projectile's position to the
// calculated one
//this code angles the projectile sprite
double PI = 3.14159265;
float dx = pX - hero.getX();
float dy = pY - hero.getY()-50;
double Radius = Math.atan2(dy,dx);
double Angle = Radius * 180 / PI;
projectile.setRotation((float)Angle); // sets the angle of the projectile
//Move modifier for projectile
MoveByModifier movMByod = new MoveByModifier(realMoveDuration, realX, realY);
final ParallelEntityModifier par = new ParallelEntityModifier(movMByod);
DelayModifier dMod = new DelayModifier(0.001f);
dMod.addModifierListener(new IModifierListener<IEntity>() {
@Override
public void onModifierStarted(IModifier<IEntity> arg0, IEntity arg1) {
}
@Override
public void onModifierFinished(IModifier<IEntity> arg0, IEntity arg1) {
// TODO Auto-generated method stub
shootingSound.play();
projectile.setVisible(true);
projectile.setPosition(hero.getX(), hero.getY() + hero.getHeight() / 2);
projectilesToBeAdded.add(projectile);
projectile.animate(50);
}
});
SequenceEntityModifier seq = new SequenceEntityModifier(dMod, par);
projectile.registerEntityModifier(seq);
projectile.setVisible(false);
mMainScene.attachChild(projectile, 1);我已经把英雄放在右边了。我需要做什么才能让投射物正确地向左移动?
非常感谢你的帮助。
MWM
发布于 2012-03-14 20:12:17
您不应该以这种方式使用DelayModifier。相反,为您的精灵创建PhysicsHandler,然后将速度设置为PhysicsHandler。类似于:
PhysicsHandler phys = new PhysicsHandler();
projectile.registerUpdateHandler(phys);
phys.setVelocityX(50);这将负责移动你的弹丸。你也可以用同样的方法在物理处理器上设置加速。因此,如果你将初始速度设置为向上和向左,然后将加速度设置为向下,射弹将首先向左和向上飞行,然后逐渐下降。而且你不需要自己做任何计算。
发布于 2013-01-24 01:00:47
此代码与http://jimmaru.wordpress.com/2011/09/28/andengine-simple-android-game-tutorial/中的代码类似。如果是,请尝试以下代码:
private void shootProjectile(final float pX, final float pY) {
int side = 1;
int offX = (int) (pX - (hero.getX()));
int offY = (int) (pY - (hero.getY() + hero.getHeight()/2));
if (offX <= 0){
side=-1
}
// position the projectile on the player and set up path
projectile = pPool.obtainPoolItem();
int realX = (int) (mCamera.getWidth() - (hero.getX() ) ) * side;
....我在link的代码中遇到了同样的问题,有了这个改变,我可以在屏幕中间用一个球员在两边投篮。
https://stackoverflow.com/questions/9651054
复制相似问题