| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 652 人关注过本帖
标题:帮我看下我的问题出在哪里?为什么会是这样的效果
只看楼主 加入收藏
gjq0072504
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-12-2
收藏
 问题点数:0 回复次数:0 
帮我看下我的问题出在哪里?为什么会是这样的效果
Sprite.rar (21 KB)

代码在这里


import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class Game_MIDlet5 extends MIDlet {
        private MyGameCanvas myc;

        Display dis;

        public Game_MIDlet5() {
                myc = new MyGameCanvas();
                dis = Display.getDisplay(this);
                dis.setCurrent(myc);
        }

        protected void startApp() throws MIDletStateChangeException {

        }

        protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

        }

        protected void pauseApp() {

        }

        class MyGameCanvas extends GameCanvas implements Runnable {
                boolean up = false, down = false, left = false, right = false;

                Image image;

                Graphics g;

                public Sprite sprite;

                int n = 0;

                int z = 0;

                int key;

                public MyGameCanvas() {
                        super(false);
                        g = this.getGraphics();
                        try {

                                image = Image.createImage("/yidong.png");

                                sprite = new Sprite(image, 24, 29);
                                sprite.setFrame(6);
                                int nPosx = getWidth() / 2 - 25;
                                int nPosy = getHeight() / 2 - 25;
                                sprite.setPosition(nPosx, nPosy);
                        } catch (Exception e) {
                                e.printStackTrace();
                        }

                        image = null;
                        start();// 调用启动方法
                }

                public void paint(Graphics g) {
                        g.setColor(255, 255, 255);
                        g.fillRect(0, 0, getWidth(), getHeight());
                        sprite.paint(g);
                        
                        
                        int n = sprite.getFrame();

                        if (n >= 8) {
                                n = 0;
                        }
                        sprite.setFrame(n);
                }

               
                public void cycle() {
                        if (sprite.getX() > 0 && sprite.getX() < getWidth() - 16 && sprite.getY() > 0
                                        && sprite.getY() < getHeight() - 32) {
                                if (up) {
                                        sprite.setPosition(sprite.getX(), sprite.getY() - 2);// 上
                                        sprite.setFrameSequence(new int[] { 0, 1, 2 });
                                }
                                if (down) {
                                        sprite.setPosition(sprite.getX(), sprite.getY() + 2);// 下
                                        sprite.setFrameSequence(new int[] { 6, 7, 8 });

                                }
                                if (left) {

                                        sprite.setPosition(sprite.getX() - 2, sprite.getY()); // 左
                                        sprite.setFrameSequence(new int[] { 9, 10, 11 });

                                }
                                if (right) {

                                        sprite.setPosition(sprite.getX() + 2, sprite.getY()); // 右
                                        sprite.setFrameSequence(new int[] { 3, 4, 5 });
                                }
                        }
                }

                public void keyPressed(int keycode) {

                        switch (getGameAction(keycode)) {
                        case UP:
                                up = true;
                                break;
                        case DOWN:
                                down = true;
                                break;
                        case LEFT:
                                left = true;
                                break;
                        case RIGHT:
                                right = true;
                                break;
                        }

                }

                public void keyReleased(int keycode) {
                        switch (getGameAction(keycode)) {
                        case UP:
                                up = false;
                                break;
                        case DOWN:
                                down = false;
                                break;
                        case LEFT:
                                left = false;
                                break;
                        case RIGHT:
                                right = false;
                                break;
                        }
                }

                public void run() {
                        while (true) {
                                try {
                                        System.out.println("55555555555");
                                        repaint();
                                        Thread.sleep(200);
                                        cycle();

                                } catch (Exception ex) {
                                }
                        }

                }

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

        }
        

}
搜索更多相关主题的帖子: 效果 
2010-12-08 17:22
快速回复:帮我看下我的问题出在哪里?为什么会是这样的效果
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030487 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved