android游戏开发5-5 实现背景图的沿x轴移动
来源:互联网 发布:linux重启ntp服务命令 编辑:程序博客网 时间:2024/06/13 05:20
package com.mz.demo;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;public class MySurfaceView extends SurfaceView implements Callback, Runnable { // 1.声明变量、对象 private Paint paint; private Canvas canvas; private SurfaceHolder sfh; private Bitmap map1, map2; private int x = 0; private int y = 0; private int x2 = 0; private boolean isDo = true; private Thread thread; public MySurfaceView(Context context) { super(context); paint = new Paint(); paint.setAntiAlias(true); sfh = this.getHolder(); sfh.addCallback(this); map1 = BitmapFactory.decodeResource(getResources(), R.drawable.background); map2 = BitmapFactory.decodeResource(getResources(), R.drawable.water); // TODO Auto-generated constructor stub } public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub x = this.getWidth() - map2.getWidth();// 注1 y = this.getHeight() - map2.getHeight(); x2 = x - map2.getWidth(); // Mydraw(); thread = new Thread(this);//***这里要注意因为采用的是继承接口的方式,所以要在括号里些写this 总感觉期末可能作为改错题 thread.start(); } private void Mydraw() { // TODO Auto-generated method stub canvas = sfh.lockCanvas();//锁定画布 canvas.drawColor(Color.WHITE); canvas.drawBitmap(map2, x, y, paint); canvas.drawBitmap(map2, x2, y, paint); sfh.unlockCanvasAndPost(canvas);//解锁 } public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } public void run() { // TODO Auto-generated method stub while (isDo) { Mydraw(); logic(); try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } } } private void logic() { // TODO Auto-generated method stub if(x>=this.getWidth()){ x=this.getWidth() - map2.getWidth()-map2.getWidth()+1;//+1的作用是去点两个图pain连接处的间隙 }else{ x+=5; } if(x2>=this.getWidth()){ x2=this.getWidth() - map2.getWidth()-map2.getWidth()+1; }else{ x2+=5; } }}
注释1:
1 0
- android游戏开发5-5 实现背景图的沿x轴移动
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 05.游戏界面 主角飞机的移动
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 07.游戏界面 敌机的产生与移动
- cocos2d-x-3.2塔防游戏开发1:背景,地图,obj,产生移动NPC的实现
- android 游戏 实战打飞机游戏 实现无限循环的背景图(2)
- android游戏开发一:背景图片的移动
- CocoStudio ------ 基于Cocos2d-X的移动游戏开发工具集合
- Android 简单游戏实现笑脸的移动与人物移动
- Android动画背景图自动播放的实现
- Android动画背景图自动播放的实现
- Android动画背景图自动播放的实现
- Android游戏开发中对象移动的例子
- 转载 Android游戏开发中对象移动的例子
- Android移动开发-属性动画的实现
- Android基于box2d开发弹弓类游戏[二]-------------游戏界面的搭建&移动游戏场景
- Cocos2d-x Lua游戏开发之Lua oo 的实现
- Cocos2d-X开发中国象棋《六》游戏开始功能的实现
- android游戏中实现对象的触屏移动
- javascript闭包
- c++第五次作业
- Spring Security 4 整合Hibernate 实现持久化登录验证(带源码)
- 面向项目(二)—— visual studio 的使用
- 海康转码
- android游戏开发5-5 实现背景图的沿x轴移动
- 直接插入排序
- javascript作用域链
- 安卓开发基础之java基础中this关键字解析
- android的消息机制之学习笔记
- ubuntu系统root登录
- Linux追求记(1)
- Spark生态之Alluxio学习9---Auuxio(Tachyon)的优点
- STL_学习_vector容器源码解析