Android 中的移位运算符语法错误
只是想知道你是否可以在 android 中使用移位运算符,我在尝试时遇到语法错误。运算符是>> << >>>>> 。如果不支持,那是他们的 android sdk 等效项吗?
编辑:这是我正在使用的代码。我正在尝试进行每像素碰撞检测并正在尝试这一点。
public void getBitmapData(Bitmap bitmap1, Bitmap bitmap2){
int[] bitmap1Pixels;
int[] bitmap2Pixels;
int bitmap1Height = bitmap1.getHeight();
int bitmap1Width = bitmap1.getWidth();
int bitmap2Height = bitmap1.getHeight();
int bitmap2Width = bitmap1.getWidth();
bitmap1Pixels = new int[bitmap1Height * bitmap1Width];
bitmap2Pixels = new int[bitmap2Height * bitmap2Width];
bitmap1.getPixels(bitmap1Pixels, 0, bitmap1Width, 1, 1, bitmap1Width - 1, bitmap1Height - 1);
bitmap2.getPixels(bitmap2Pixels, 0, bitmap2Width, 1, 1, bitmap2Width - 1, bitmap2Height - 1);
// Find the first line where the two sprites might overlap
int linePlayer, lineEnemy;
if (ninja.getY() <= enemy.getY()) {
linePlayer = enemy.getY() - ninja.getY();
lineEnemy = 0;
} else {
linePlayer = 0;
lineEnemy = ninja.getY() - enemy.getY();
}
int line = Math.max(linePlayer, lineEnemy);
// Get the shift between the two
int x = ninja.getX() - enemy.getX();
int maxLines = Math.max(bitmap1Height, bitmap2Height);
for (; line <= maxLines; line ++) {
// if width > 32, then you need a second loop here
long playerMask = bitmap1Pixels[linePlayer];
long enemyMask = bitmap2Pixels[lineEnemy];
// Reproduce the shift between the two sprites
if (x < 0) playerMask << (-x);
else enemyMask << x;
// If the two masks have common bits, binary AND will return != 0
if ((playerMask & enemyMask) != 0) {
// Contact!
Log.d("pixel collsion","we have pixel on pixel");
}
}
}
Was just wondering if you could use the shift operator in android I am getting a syntax error when trying it. the operator is >> << >>> . If it doesn't support it is their an android sdk equivalent?
EDIT: here is the code i am using. I am trying to do a per pixel collision detection and was trying this out.
public void getBitmapData(Bitmap bitmap1, Bitmap bitmap2){ int[] bitmap1Pixels; int[] bitmap2Pixels;
int bitmap1Height = bitmap1.getHeight(); int bitmap1Width = bitmap1.getWidth(); int bitmap2Height = bitmap1.getHeight(); int bitmap2Width = bitmap1.getWidth(); bitmap1Pixels = new int[bitmap1Height * bitmap1Width]; bitmap2Pixels = new int[bitmap2Height * bitmap2Width]; bitmap1.getPixels(bitmap1Pixels, 0, bitmap1Width, 1, 1, bitmap1Width - 1, bitmap1Height - 1); bitmap2.getPixels(bitmap2Pixels, 0, bitmap2Width, 1, 1, bitmap2Width - 1, bitmap2Height - 1); // Find the first line where the two sprites might overlap int linePlayer, lineEnemy; if (ninja.getY() <= enemy.getY()) { linePlayer = enemy.getY() - ninja.getY(); lineEnemy = 0; } else { linePlayer = 0; lineEnemy = ninja.getY() - enemy.getY(); } int line = Math.max(linePlayer, lineEnemy); // Get the shift between the two int x = ninja.getX() - enemy.getX(); int maxLines = Math.max(bitmap1Height, bitmap2Height); for (; line <= maxLines; line ++) { // if width > 32, then you need a second loop here long playerMask = bitmap1Pixels[linePlayer]; long enemyMask = bitmap2Pixels[lineEnemy]; // Reproduce the shift between the two sprites if (x < 0) playerMask << (-x); else enemyMask << x; // If the two masks have common bits, binary AND will return != 0 if ((playerMask & enemyMask) != 0) { // Contact! Log.d("pixel collsion","we have pixel on pixel"); } } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要附加到字符串,除非将算术运算放在括号中,否则您会收到错误:
If you're appending to a string you'll get an error unless you put the arithmetic operations in parentheses:
Android 使用的 Java 确实支持按位运算。 这是一个方便的指南。
Java, which is used by Android does support bitwise operations. Here's a handy guide.