canvas教程

JBox2d入门学习二 我的小鸟(3)

字号+ 作者:H5之家 来源:H5之家 2015-11-11 13:47 我要评论( )

//创建箱子的图像 if(bmbox == null) { bmbox = BitmapFactory.decodeResource(getResources(), R.drawable.xxx); bmbox = Bitmap.createScaledBitmap(bmbox, (int)(20*density), (int)(20*density),true); } //遍

//创建箱子的图像
if(bmbox == null)
{
bmbox = BitmapFactory.decodeResource(getResources(), R.drawable.xxx);
bmbox = Bitmap.createScaledBitmap(bmbox, (int)(20*density), (int)(20*density),true);
}

//遍历刚体链表,判断刚体类型并且分别进行绘画
if(arrBody != null)
{
for(int i = 0;i< arrBody.size();i++)
{
Body body = arrBody.get(i);
Vec2 point = body.getPosition();
if(body.getUserData().toString().substring(0,7).equals(“FlyBall”))
drawBall(point.x,point.y,10*density);//绘画小鸟
else
drawThings(point.x,point.y);//绘画物体

}
}
drawBorder();//绘画边界
drawSlingshot();//绘画弹弓
}
private void drawThings(float x,float y)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.YELLOW);
x = x*10;//这里*10是因为我们要对box2d世界与屏幕之间的比例进行换算,这个比例是我们自己定义的,是上文当中的 private final static int RATE = 10;// 屏幕到现实世界的比例
y = y*10;
canvas.drawBitmap(bmbox, x-10*density, y-10*density, paint);
//canvas.drawRect(x-10*density, y-10*density, x+10*density, y+10*density, paint);
}
private void drawBall(float x,float y,float radius)
{

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
x = x*10;
y = y*10;
//canvas.drawCircle(x, y, radius, paint);
canvas.drawBitmap(bmbird, x-10*density, y-10*density, paint);
}
private void drawBorder()
{
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
canvas.drawRect(0,310*density,550*density,320*density,paint);
}
private void drawSlingshot(){
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
canvas.drawLine(50, 260*density, 75, 285*density, paint);
canvas.drawLine(75, 285*density, 100, 260*density, paint);
canvas.drawLine(75, 285*density, 75, 310*density, paint);
if(singleshot.y != 0)
{
paint.setColor(Color.RED);
canvas.drawLine(50, 260*density, singleshot.x, singleshot.y, paint);
canvas.drawLine(100, 260*density, singleshot.x, singleshot.y, paint);
canvas.drawBitmap(bmbird, singleshot.x-10*density, singleshot.y-10*density, paint);
}
}
}

 

 

由于网上资料太少,很多地方都是比对着Box2d的资料摸索出来的,有些地方可能理解的不够透彻,望大家谅解..代码也没有经过什么优化,大家不要歧视我..希望对大家能够有用。

源代码,想要的童鞋拿去吧..

FlyBallDemo源代码

还有入门学习一的源代码

Ball源代码

This entry was posted in 易网联信 and tagged JBOX, JBox2d例子, JBox2d入门学习, JBOX2D模拟愤怒小鸟. Bookmark the .

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • SketchBook 2014 基础入门视频教程

    SketchBook 2014 基础入门视频教程

    2017-05-01 11:00

  • 学习慕课网canvas倒计时实例笔记

    学习慕课网canvas倒计时实例笔记

    2017-04-30 14:01

  • 从一个画板demo学习canvas

    从一个画板demo学习canvas

    2017-04-30 13:00

  • canvas游戏开发学习之四:应用图像

    canvas游戏开发学习之四:应用图像

    2017-04-29 16:00

网友点评
"