If have login problems remove cookies and clear browser cache.



Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUTORIAL] Creating class in J2ME SDK Mobile
05-20-2021, 09:26 PM (This post was last modified: 05-20-2021 09:42 PM by JoseskVolpe.)
Post: #1
Information [TUTORIAL] Creating class in J2ME SDK Mobile
¿What is J2ME SDK Mobile?
J2ME SDK Mobile is a tool that lets you create Java ME games and applications in your phone, without the need of a PC.
You can see more informations and download it here: [APP] J2ME SDK Mobile

Introduction
I've had received some messages that correlates with the "¿how do i begin?" issue. It's about how do you create a new class in your project

¿What are classes?
First, Java is a object-oriented programming, and it works by polimorphism
It means that you can script a object a single time, and then just extend it to change some of it's attributes

Polimorphism
So, for example, if i wanna create a NPC
J2ME native's libraries has a Sprite class. So i could extend it to make it as Sprite class

So, i can create a new .java file with this code. J2ME SDK Mobile will already partially generate this code when i create a new class
Code:
package my.game.package;

import javax.microedition.lcdui.game.Sprite; //Import native J2ME's Sprite class to this script

//Creates a new Sprite class
public class NPC extends Sprite{

//Standard variables any NPC may have. I'm letting it here just as an example. You'll create your own variables you'll need

public static final byte NPC_TYPE_PLAYER=0;
public static final byte NPC_TYPE_ALLY=1;
public static final byte NPC_TYPE_ZOMBIE=2;

protected byte NPC_TYPE;
protected int HP, maxHP;

/**
* Constructor
*/
public NPC(){
}

//Notice i've implemented my damage method on here
public damage(int hpLoss){
  HP-=hpLoss; //Makes the NPC loss health
}

}

Additionally, i can now create a Ally, Player and Zombie class with their own variables, extending any feature i can create on my parent classes.
So, if i modify my NPC class. Both my Ally, Zombie and Player classes will extend these modifications easier and faster to program, avoiding the need to script the same thing many times.

Player:
Code:
package my.game.packages.npcs;

import my.game.packages.NPC; //Import my last NPC class i've created before

public class Player extends NPC{

public Player(){
  //Defines all my protected variables
  this.NPC_TYPE = NPC.NPC_TYPE_PLAYER;
  this.HP = 100;
  this.maxHP=100;
}

//You can also create methods exclusively for this class ;3
public void move(int x, int y){
  //Code for movement
}
public void shoot(){
  //Code for shooting
}
}

Ally:
Code:
package my.game.packages.npcs;

import my.game.packages.NPC; //Import my last NPC class i've created before

public class Ally extends NPC{

public Ally{){
  //Defines all my protected variables
  this.NPC_TYPE = NPC.NPC_TYPE_ALLY;
  this.HP = 100;
  this.maxHP=100;
}
}

Zombie:
Code:
package my.game.packages.npcs;

import my.game.packages.NPC; //Import my last NPC class i've created before

public class Zombie extends NPC{

public Zombie{){
  //Defines all my protected variables
  this.NPC_TYPE = NPC.NPC_TYPE_ZOMBIE;
  this.HP = 40;
  this.maxHP=40;
}

public attack(NPC npc){
  npc.damage(15); //Damage the designed NPC. Notice i've used a extended method every NPC class has
}

}

From this, i've created this structure:
  • Project
    • my
      • game
        • packages
          • NPC extends Sprite
          • npcs
            • Player extends NPC
            • Ally extends NPC
            • Zombie extends NPC


Creating a new class tutorial
  • Select "Projects" in the top menu
    [Image: qOrcyKg.jpg]
  • Open "Source Packages"
    [Image: yVOV0Nb.jpg]
  • Go to the package you want to insert your new class, or create a new one, and press your Options Soft Button
    [Image: xg0RIYp.jpg]
  • Select "New"
    [Image: zFHJpwP.jpg]
  • Select "Java Class" and click your Next Soft Button
    [Image: UvMHgkE.jpg]
  • Insert your new class name (anything you want, avoid spaces and special characters), and click your Next Soft Button
    [Image: Q98eDl4.jpg]

  • Select "Finish"
    [Image: 0VT30F5.jpg]
  • Done, now just script :3
    [Image: 0rQxcBG.jpg]

Errata
I cannot edit it, so i'll levae it here

I've wrote it:
Code:
public damage(int hpLoss){
  HP-=hpLoss; //Makes the NPC loss health
}

It's wrong, because damage() is a method, and is written as a constructor
We should do this instead:

Code:
public void damage(int hpLoss){
  HP-=hpLoss; //Makes the NPC loss health
}

[size=xx-small]Signature[/size]
[URL=https://www.exophase.com/user/JoseskVolpe/][IMG]https://card.exophase.com/2/0/45687.png?1612237163[/IMG][/URL]

[align=justify][size=x-small][b]OwO ¿¿¿What is this??? *wags tail*
Take my beret plz 7w7 ~[/b][/size][/align]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)