Pages

Thursday, October 28, 2010

about canvas class and using it in mobile game

If you want using keys or touch or graphics in your game you should use canvas class.Canvas class have predefined function for detect key press or drag or screen  touch and also important paint function .First you should add new canvas class with right click on your project and go new and select MIDP canvas…..

In this class you’ll see some function like this

protected  void keyPressed(int keyCode) {
   }
  
   /**
    * Called when a key is released.
    */
   protected  void keyReleased(int keyCode) {
   }

   /**
    * Called when a key is repeated (held down).
    */
   protected  void keyRepeated(int keyCode) {
   }

……………..

……………

…………….

……………

they are contains code that running when key pressed or released or repeated or…you can add code like draw string in keypress and create text when key pressed .Now you should running canvas in your game .For this purpose you should add code like this in startApp function in your middlet.

 Display.getDisplay(this).setCurrent(new MIDPCanvas());

jus thisWinking smile

in next post I’ll describe how you can detect fire,back,numbers,..keys and pointer into your software 

Friday, October 22, 2010

First view of java mobile app code and change it

When you build a java mobile game in software(like netbeans IDE or eclipse..)

first thing you‘ll see is  a middlet class .Middlet class used for handling start and

initialize and interface of java mobile applications. In netbeans you have sample

screen that can add button or text or image or like that .just you should drag your desired items from right side to screen and then go to source

 

netbeansscreen

screen is interface of your project in netbeans

For example you  have pre defined string seeing in screen and want changed it when middlet started. Code is like this

public void startMIDlet() {
       // write pre-action user code here
       switchDisplayable(null, getForm());
             stringItem.setText("my name is dawood");
       // write post-action user code here
   }

startMIDlet is function that using in every middlet .In netbeans for adding interface to screen using a form , should calling when middlet started.For this purpose have this code switchDisplayable(null, getForm());

and then add this for changing text stringItem.setText("my name is dawood");

and run it in emulator

emulator1

in start for do more thing in java games you should add canvas class that i’ll describe later

Saturday, October 16, 2010

about objects in your game(in Android)

  Game contains objects and environment and you should now how coding them.They are defined by class keyword .see this code for example

package com.example.helloandroid;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class HelloAndroid extends Activity

{

/** Called when the activity is first created.

*/ @Override

  public void onCreate(Bundle savedInstanceState)

  {

  super.onCreate(savedInstanceState);

TextView tv =  new TextView (this)tv.setText("Hello, Android"); setContentView (tv);

}

}

this is sample code for showing hello word in android( get from here)

Package contains you project content.For running your project in ide you should add required codes.Import keyword using for add this code.Public means that your object is visible to project or package.helloAndroid is your main project(environment) and onCreate is function of objects that used to create a text in device(i’ll describe code later,see source for some help for now).Your code can be changed on base of your algorithm. Your algorithm can be like this :

1.add codes for running games

2.create  HelloAndroid environment(project)

3.set text “hello, Android”

4.show them

Now you can add a line to algorithm “set text when you clicked on 5” and change code also.For now is sufficient ,I’ll describe more in following posts.For coding using eclipse and adt plugin and android sdk  and run your project and see result like this

hello_world_5

linkwithin

Related Posts Plugin for WordPress, Blogger...