001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc.
003: *
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: * * Neither the name of Sun Microsystems, Inc. nor the names of its
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
023: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
024: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package demo;
034:
035: import javax.microedition.lcdui.game.GameCanvas;
036: import javax.microedition.lcdui.game.Sprite;
037:
038: /**
039: * Animate a sprite on canvas using a simple algorithm
040: * to recover from collisions.
041: *
042: * @author Karel Herink
043: */
044: public class SpriteRandomMovement implements Runnable {
045:
046: private static final int SPEED = 3;
047: private DemoGameCanvas canvas;
048: private Sprite sprite;
049: private byte previousDirection = GameCanvas.DOWN;
050: private byte direction = GameCanvas.DOWN;
051: private boolean interrupted;
052: private int[] downSeq;
053: private int downTrans;
054: private int[] upSeq;
055: private int upTrans;
056: private int[] leftSeq;
057: private int leftTrans;
058: private int[] rightSeq;
059: private int rightTrans;
060:
061: public SpriteRandomMovement(DemoGameCanvas canvas, Sprite sprite) {
062: this .canvas = canvas;
063: this .sprite = sprite;
064: }
065:
066: public void setSequences(int[] downSeq, int downTrans, int[] upSeq,
067: int upTrans, int[] leftSeq, int leftTrans, int[] rightSeq,
068: int rightTrans) {
069: this .downSeq = downSeq;
070: this .downTrans = downTrans;
071: this .upSeq = upSeq;
072: this .upTrans = upTrans;
073: this .leftSeq = leftSeq;
074: this .leftTrans = leftTrans;
075: this .rightSeq = rightSeq;
076: this .rightTrans = rightTrans;
077: }
078:
079: public void stop() {
080: this .interrupted = true;
081: }
082:
083: public void run() {
084: while (!this .interrupted) {
085: if (this .direction == GameCanvas.DOWN) {
086: if (this .previousDirection != this .direction) {
087: this .sprite.setFrameSequence(this .downSeq);
088: this .sprite.setTransform(this .downTrans);
089: this .previousDirection = this .direction;
090: }
091: this .sprite.move(0, SPEED);
092: if (this .canvas.spriteCollides(this .sprite)) {
093: this .sprite.move(0, -SPEED);
094: this .direction = GameCanvas.LEFT;
095: continue;
096: }
097: } else if (this .direction == GameCanvas.UP) {
098: if (this .previousDirection != this .direction) {
099: this .sprite.setFrameSequence(this .upSeq);
100: this .sprite.setTransform(this .upTrans);
101: this .previousDirection = this .direction;
102: }
103: this .sprite.move(0, -SPEED);
104: if (this .canvas.spriteCollides(this .sprite)) {
105: this .sprite.move(0, SPEED);
106: this .direction = GameCanvas.RIGHT;
107: continue;
108: }
109: } else if (this .direction == GameCanvas.LEFT) {
110: if (this .previousDirection != this .direction) {
111: this .sprite.setFrameSequence(this .leftSeq);
112: this .sprite.setTransform(this .leftTrans);
113: this .previousDirection = this .direction;
114: }
115: this .sprite.move(-SPEED, 0);
116: if (this .canvas.spriteCollides(this .sprite)) {
117: this .sprite.move(SPEED, 0);
118: this .direction = GameCanvas.UP;
119: continue;
120: }
121: } else if (this .direction == GameCanvas.RIGHT) {
122: if (this .previousDirection != this .direction) {
123: this .sprite.setFrameSequence(this .rightSeq);
124: this .sprite.setTransform(this .rightTrans);
125: this .previousDirection = this .direction;
126: }
127: this .sprite.move(SPEED, 0);
128: if (this .canvas.spriteCollides(this .sprite)) {
129: this .sprite.move(-SPEED, 0);
130: this .direction = GameCanvas.DOWN;
131: continue;
132: }
133: }
134: try {
135: Thread.sleep(300);
136: } catch (InterruptedException ex) {
137: ex.printStackTrace();
138: }
139: }
140: }
141: }
|