Android游戏引擎《Rokon》学习笔记三:精灵类Sprites的使用

来源:互联网 发布:山东出版社行知天下 编辑:程序博客网 时间:2024/05/22 15:05

欢迎来到第二篇Rokon教程()

在本教程里,你将会学习怎样添加一个精灵类,并且移动他们。

假定你已经有了一定的Java编程、Android编程的基础,如果没有,来这里(需翻墙,方法略)。

在这个教程里我将会用Eclipse作为开发工具,如果你用其他的开发工具,照样可以继续阅读。

我们继续上一节的“Hello World”教程,所以假定你已经学习过上一节的内容并且已经建立一个Rokon项目了。

(在教程的最后你将会下载到所涉及到的源码)

打开Eclipse转到Rokon项目。

本教程我们就爱那个会修改

‘GameScene’ 和 ‘Textures’这两个类。

 

 

 

 

 

 

 

我们来添加几个精灵类,因此我们需要家在另外的texture,所以在 TextTures类里,添加如下语句。

‘atlas.insert(bob = new Texture("bob.png"));’ 像这样:

view plain
  1. package com.rokonexamples.sprite;  
  2. import com.stickycoding.rokon.Texture;  
  3. import com.stickycoding.rokon.TextureAtlas;  
  4. public class Textures {  
  5.     public static TextureAtlas atlas;  
  6.     public static Texture background, bob;  
  7.     public static void load() {  
  8.         atlas = new TextureAtlas();  
  9.         atlas.insert(background = new Texture("background.png"));  
  10.         atlas.insert(bob = new Texture("bob.png"));  
  11.         atlas.complete();  
  12.     }  
  13. }  
在你的 ‘GameScene’ 类里,像这样修改构造方法:
view plain
  1. public GameScene() {  
  2.     super(13);  
  3.     setBackground(background = new FixedBackground(Textures.background));  
  4.     // Create the Bob sprites.  
  5.     bob = new Sprite(100220, Textures.bob.getWidth(), Textures.bob.getHeight());  
  6.     bob.setTexture(Textures.bob);  
  7.     bob2 = new Sprite(100180, Textures.bob.getWidth(), Textures.bob.getHeight());  
  8.     bob2.setTexture(Textures.bob);  
  9.     bob3 = new Sprite(100260, Textures.bob.getWidth(), Textures.bob.getHeight());  
  10.     bob3.setTexture(Textures.bob);  
  11.     // Add the Bob sprites to the first layer.  
  12.     add(0, bob);  
  13.     add(0, bob2);  
  14.     add(0, bob3);  
  15. }  
注意我们把第一行的 ‘super()’ 改为to ‘super(1, 3)’, 意为我们会使用一个层,并且层里包含三个精灵。

然后我们添加三个精灵到场景里。

bob = new Sprite(100, 220, Textures.bob.getWidth(), Textures.bob.getHeight());

bob.setTexture(Textures.bob);
bob2 = new Sprite(100, 180, Textures.bob.getWidth(), Textures.bob.getHeight());
bob2.setTexture(Textures.bob);
bob3 = new Sprite(100, 260, Textures.bob.getWidth(), Textures.bob.getHeight());
bob3.setTexture(Textures.bob);

方法里前两个参数为精灵的X和Y位置,第三个第四个参数为他们的大小,但由于我们不希望精灵被拉伸,所以我们要设置成和textures一样的尺寸。

请注意,你可以像我这样为多个精灵使用一个textures。

添加精灵到sence场景里。
add(0, bob);
add(0, bob2);
add(0, bob3);

参数0的意思为添加到第一个层。因为我们只有一个层,所以可以写成 ‘add(bob)’。

如果你现在运行项目,你将会看到一个和上一节一样的静态的背景,并且多了三个精灵。

现在让精灵们移动,我们需要用到 ‘onGameLoop()’ 方法。

这个方法每帧都会被调用,你可以在里面更新游戏逻辑。所以添加 ‘onGameLoop()’ 方法:

view plain
  1. public void onGameLoop() {  
  2.     bob.x += 1;  
  3.     if (bob.x >= MainActivity.GAME_WIDTH)  
  4.     {  
  5.         bob.x = 0;  
  6.     }  
  7.     bob2.rotate(2);  
  8. }  
第一句让第一个精灵匀速?移动到右边(通过改变x值),如果它移除了屏幕我们让他移动回来。

我们让第二个精灵每帧旋转2度 ‘bob2.rotate(2)’.

你也可以让精灵移动一段时间。

添加   ‘onReady()’ 方法:

view plain
  1. public void onReady() {  
  2.     bob3.moveTo(4501005000);  
  3. }  
它的意思是:取代直接移动精灵岛某一个点,改为播放一段时间的动画。

( 这种方法会让精灵移动到 x 450, y 100 ,用时 5000ms 即5 seconds)。

好了就这些,只需要添加到Bob图片到 assets/textures 文件夹下,运行。

如果一切正常,你将会看到下图(略)