Selasa, 31 Mei 2011

Penggunaan Sprite pada Mobile

Pada umumnya sprite di pakai untuk pergerakan objek, Sprite mengacu kepada kumpulan frame.
Buat 2 class, yang pertama class canvas dan yang kedua class midlet .

Inilah contoh script class canvasnya  :



import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.*;

public class SpriteMoveCanvas extends GameCanvas implements Runnable
{
    boolean isPlay;
    int curX, curY, cX, cY;
    int w, h;
    Image gbr1, gbr2;
    Sprite obj1, obj2, obj3;

    public SpriteMoveCanvas()
    {
        super(true);
        w = getWidth();
        h = getHeight();
        curX = 0;
        curY = h/2;
        cX = 210;
        cY = h/2;
        try
        {
            gbr1 = Image.createImage("/gambar/transparent.png");
            gbr2 = Image.createImage("/gambar/nontransparent.png");
        }catch(IOException ie){
            ie.printStackTrace();
        }

        obj1 = new Sprite(gbr1,32,32);
        obj2 = new Sprite(gbr2,32,32);
        obj3 = new Sprite(gbr1,32,32);
    }

    public void start()
    {
        isPlay = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void stop()
    {
        isPlay = false;
    }

    public void drawScreen(Graphics g)
    {
        g.setColor(0,0,255);
        g.fillRect(0, 0, w, h);
        obj1.setPosition(curX, curY);//untuk posisi gambar di tengah
        obj3.setPosition(cX, cY);
        obj1.paint(g);
        obj3.paint(g);
        obj2.paint(g);
        flushGraphics();
    }

    public void input()
    {
        int key = getKeyStates();
        //kiri
        if(key == 4)
        {
            if(curX > 0)
            {
            curX = curX - 1;
            obj1.setFrame(1);
            }

        }else if (key == 32) //kanan
        {
            if(curX < (getWidth() - obj1.getWidth()));
            curX += 1;
            obj1.setFrame(3);
        }

        //atas
        if(key == 2)
        {
            if(curY > 0)
            curY -= 1;
            obj1.setFrame(2);
        }else if(key == 64) //bawah
        {
            if(curY < (getHeight() - obj1.getHeight()))
            curY += 1;
            obj1.setFrame(4);
        }
    }

//    public void AU()
//    {
//        if (curY == h/2)
//        {
//            curX += 1;
//            obj1.setFrame(3);
//        }else if(cY == h/2)
//            {
//                cX -= 1;
//                obj3.setFrame(1);
//            }
//
//        if(curX == 0)
//        {
//            curY += 1;
//            obj1.setFrame(4);
//        }else if(cX == w/2)
//            {
//                cY += 1;
//                obj3.setFrame(4);
//            }
//
//        if(curX == w/2)
//        {
//            curY -= 1;
//            obj1.setFrame(2);
//        }else if(cX == 0)
//            {
//                cY -= 1;
//                obj3.setFrame(3);
//            }
//
//        if(curY == 0)
//        {
//            curX -= 1;
//            obj1.setFrame(1);
//        }else if(cY == getHeight())
//            {
//                cX -= 1;
//                obj3.setFrame(4);
//            }
//    }

    public void run()
    {
        Graphics g = getGraphics();
        while(isPlay == true)
        {
            input();
//            AU();
            drawScreen(g);
            try
            {
                Thread.sleep(10);
            }catch(InterruptedException ie){
                ie.printStackTrace();
            }
        }
    }
}

dan ini script untuk class midletnya :

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;

/**
 * @author Administrator
 */
public class ShowSprite extends MIDlet
{
    Display d;
    public void startApp()
    {
        d = Display.getDisplay(this);
        SpriteMoveCanvas s = new SpriteMoveCanvas();
        s.start();
        d.setCurrent(s);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

Tampilannya akan seperti ini :



Tekan tombol navigasi kanan, kiri atas dan bawah, maka objectnya pun akan bergerak ke arah yang sesuai dengan tombol navigasi yang ditekan.

Tidak ada komentar:

Posting Komentar