在旋转的android游戏精灵 [英] Rotate a sprite in game android
本文介绍了在旋转的android游戏精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现从位置移动精灵(X,Y)来定位ACTION_DOWN(X1,Y1),但我不能转动它。请帮助我.Thanks
这是我的code:
公共雪碧(GameView gameView,BMP位图){ this.gameView = gameView;
this.bmp = BMP;
this.width = bmp.getWidth()/ BMP_COLUMNS; //创建宽度,高度
this.height = bmp.getHeight()/ BMP_ROWS;
随机RND =新的随机(System.currentTimeMillis的());
X = rnd.nextInt(gameView.getWidth() - bmp.getWidth());
Y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
}公共无效的onDraw(帆布油画){ 字模=新的Matrix();
matrix.postTranslate(X,Y);
浮DX = X1-X;
浮DY = Y1-Y;
浮D =(浮点)的Math.sqrt(DX * DX + DY * DY);
VX =(浮点)(DX * 5 / D)/ 3;
VY =(浮点)(DY * 5 / D)/ 3;
如果(K == 1){
X + = VX;
Y + = VY;
}
设置currentFrame = ++设置currentFrame%BMP_COLUMNS;
INT srcX =设置currentFrame *宽度;
INT srcY = 0 *高度;
矩形SRC =新的矩形(srcX,srcY,srcX +宽度,srcY +高);
矩形DST =新的矩形(X,Y,X +宽度,Y +高);
canvas.drawBitmap(BMP,SRC,DST,NULL);
}
解决方案
在这里你去:
注意:您需要从位图转换为图像
进口java.awt中的*。
进口java.awt.geom.AffineTransform中;
进口java.awt.image.AffineTransformOp;
进口java.awt.image.BufferedImage中;/ **
*创建者克里斯在2014年3月28日。
* /
公共类雪碧{
私人图像I; 公共雪碧(形象画像){
this.i =图像;
} 私人的BufferedImage图像= NULL;
私人的Graphics2D图形= NULL; 公共无效的onDraw(帆布油画){
如果(像== NULL ||图形== NULL){
建立();
}
图形G = canvas.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0,0,image.getWidth(),image.getHeight());
//在哪里画雪碧在画布上。
INT X = 100;
INT Y = 100;
//因为显卡是Graphics2D的实例
//度数45转换为弧度。
双rotationAngle = Math.toRadians(45);
双locX = image.getWidth()/ 2;
双locY = image.getHeight()/ 2;
德克萨斯州的AffineTransform = AffineTransform.getRotateInstance(rotationAngle,locX,locY);
AffineTransformOp OP =新AffineTransformOp(德克萨斯州,AffineTransformOp.TYPE_BILINEAR); graphics.drawImage(op.filter(图像,空),0,0,NULL); g.drawImage(图像,X,Y,(int)的(image.getWidth()/ 2),(INT)(image.getHeight()/ 2),空);
} / **
*将图像了。
* /
私人无效设置(){
如果(形象!= NULL){
image.flush();
图像= NULL;
}
如果(图形!= NULL){
graphics.dispose();
显卡= NULL;
}
图像=新的BufferedImage(i.getWidth(空)* 2,i.getHeight(空)* 2,BufferedImage.TYPE_INT_ARGB);
图形= image.createGraphics();
}
}
I want implement move a sprite from position (x ,y ) to position action_down (x1 , y1) .But I can't rotate it .Please help me .Thanks
This is my code:
public Sprite(GameView gameView, Bitmap bmp) {
this.gameView = gameView;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;// create width, height
this.height = bmp.getHeight() / BMP_ROWS;
Random rnd = new Random(System.currentTimeMillis());
x = rnd.nextInt(gameView.getWidth() - bmp.getWidth());
y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
}
public void onDraw(Canvas canvas) {
Matrix matrix = new Matrix();
matrix.postTranslate(x, y);
float dx = x1-x;
float dy = y1-y;
float d = (float)Math.sqrt(dx*dx+dy*dy);
vx = (float) (dx*5/d)/3 ;
vy = (float) (dy*5/d)/3 ;
if(k==1){
x += vx ;
y += vy ;
}
currentFrame = ++currentFrame % BMP_COLUMNS;
int srcX = currentFrame * width;
int srcY = 0 * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bmp, src, dst, null);
}
解决方案
Here you go: Note: you need to convert from Bitmap to Image.
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
/**
* Created by Chris on 3/28/2014.
*/
public class Sprite {
private Image i;
public Sprite(Image image) {
this.i = image;
}
private BufferedImage image = null;
private Graphics2D graphics = null;
public void onDraw(Canvas canvas) {
if(image == null || graphics == null) {
setup();
}
Graphics g = canvas.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
//Where to draw the Sprite on the canvas.
int x = 100;
int y = 100;
//Because graphics is an instance of Graphics2D
//Converts the degrees "45" to radians.
double rotationAngle = Math.toRadians(45);
double locX = image.getWidth() / 2;
double locY = image.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationAngle, locX, locY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
graphics.drawImage(op.filter(image, null), 0, 0, null);
g.drawImage(image, x, y, (int) (image.getWidth() / 2), (int) (image.getHeight() / 2), null);
}
/**
* Sets the Image up.
*/
private void setup() {
if(image != null) {
image.flush();
image = null;
}
if(graphics != null) {
graphics.dispose();
graphics = null;
}
image = new BufferedImage(i.getWidth(null) * 2, i.getHeight(null) * 2, BufferedImage.TYPE_INT_ARGB);
graphics = image.createGraphics();
}
}
这篇关于在旋转的android游戏精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文