Android game engine Rokon study Note 5: Use of modifiers Modifiers

Source: Internet
Author: User

Lazy bones (http://blog.csdn.com/iamlazybone)

Descriptions of Rokon:

Rokon home: http://rokonandroid.com

Rokon documentation: http://rokonandroid.com/javadocs/

I have not passed the level 4 test ~ The level is limited ~ Right when learning notes ~ For reference only ~

I won't say anything about copyright ~ It is roughly the same as a general blog ~

----------------

Android game engine Rokon study Note 5:

Welcome to Chapter 4 Rokon tutorial ()

In this tutorial
You will learn how to add a Modifiers modifier class.

Suppose you already have a certain degree of Java programming and Android programming basics. If not, come here (you need to go over the wall, and the method is omitted ).

In this tutorial, I will use eclipse as a development tool. If you use other development tools, you can continue reading.

We continue with the previous tutorial, So assume that you have learned the content of the previous section and have created a Rokon project.

(At the end of the tutorial, you will download the involved source code)

 

 

You can add a modifier class to sprite to add some temporary effects. For example, red flashes are displayed when an attack occurs.

 

Open eclipse and go to the Rokon project.

Create a colormodifier. Java class.

I prefer to put all the modifier classes in an independent package to keep the project organized. But this is optional.

Colormodifier. Java class:

 

Package COM. rokonexamples. modifier. modifiers; <br/> Import COM. stickycoding. rokon. modifier; <br/> Import COM. stickycoding. rokon. sprite; <br/> public class colormodifier extends modifier {<br/> private float color; <br/> @ override <br/> Public void onstart (sprite) {<br/> color = 0; <br/>}< br/> @ override <br/> Public void onupdate (sprite) {<br/> // here you can do anything to the sprite, like move it or whatever. <br/> // but we will just do a simple color modification. <br/> Sprite. setrgb (1, color, color); <br/> color ++ = 0.1; <br/> // when the sprite's original colors has been restored, end it. <br/> If (color> = 1) <br/> end (); <br/>}< br/> @ override <br/> Public void onend (sprite) {<br/> Sprite. setrgb (1, 1, 1); <br/>}< br/>} 

 

 

How is modifier created:

'onStart()'Modifier: This method is called at the beginning. You can also reset it here.

'onUpdate()'When modifier is active, each frame of this method is called.

 

You can modify your sprites as follows:

sprite.setRGB(1, color, color);

This statement is used to change the sprite color. (1, 1, 1) is a normal color. (0, 0, 0) is completely black.

This modifier sets sprite to red. After a period of time, the system returns to normal color.

After the color is restored to normal, the modifier class deletes itself from the sprite class and then calls the end () method.

 

sprite.setRGB(1, 1, 1);

Restore sprite to normal color.

 

Now, add the modifier to Sprite.

Open your gamescene class and replace it with the following:

 

 

Package COM. rokonexamples. modifier; <br/> Import android. view. motionevent; <br/> Import COM. rokonexamples. modifier. modifiers. colormodifier; <br/> Import COM. stickycoding. rokon. scene; <br/> Import COM. stickycoding. rokon. sprite; <br/> Import COM. stickycoding. rokon. background. fixedbackground; <br/> public class gamescene extends scene {<br/> private fixedbackground background; <br/> private sprite Bob; <br/> private colormodifier modifier; <br/> Public gamescene () {<br/> super (1, 1); <br/> setbackground (background = new fixedbackground (textures. background); <br/> // create the Bob sprite <br/> Bob = new sprite (100,220, textures. bob. getwidth (), textures. bob. getheight (); <br/> Bob. settexture (textures. bob); <br/> // Add the Bob sprite to the first layer. <br/> Add (0, Bob); <br/> // and create the modifier. <br/> modifier = new colormodifier (); <br/>}< br/> @ override <br/> Public void ongameloop () {<br/>}< br/> @ override <br/> Public void ontouchup (float X, float y, motionevent event, int pointercount, int pointerid) {<br/> // Add your modifier to the sprite. <br/> Bob. addmodifier (modifier); <br/>}< br/> @ override <br/> Public void onpause () {<br/>}< br/> @ override <br/> Public void onresume () {<br/>}< br/> @ override <br/> Public void onready () {<br/>}< br/>} 

 

Now this class is bound to have two more lines:

 

modifier = new ColorModifier();
bob.addModifier(modifier);

 

Their role is very obvious. The first sentence is set up, and the second sentence is added to the genie class every time you touch the screen.

 

Remember that the modifier can be reused once it is created.

If you generate a new modifier into your sprite every time, it will bring a lot of work to GC, and the game will also feel stuck.

 

 

(You may not notice this small difference)

Well, if you run this program, every time you press the screen, your genie will become red.

 

If everything is normal, you will see:

 

 

 

You can download the source code of this example through the wall: Download

 

 

===================== You can take a ship to another tutorial ==========================

 

 

Android game engine Rokon study Note 1: Add a Rokon framework to your project

 

Study Note 2 of the android game engine Rokon: helloworld

 

Android game engine Rokon study Note 3: Use of genie sprites

 

Android game engine Rokon study NOTE 4: Add touch control

 

Android game engine Rokon study Note 5: Use of modifiers Modifiers

 

Study Note 6 of the android game engine Rokon: A small Rokon example using box2d: Rokon donate

 

Rokon: main categories and Methods

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.