Ik ben bezig met een spelletje voor een mobieltje.
Ik heb heb een canvas met sprites erin, wanneer je met het poppetje tegen een bepaalde sprite aanloopt wil ik dat je een textbox krijgt met daarin een vraag.
nu ben ik al de hele dag aan het zoeken hoe ik dat voor elkaar krijg...
Ik ben erachter gekomen dat canvas een low level api is en dat daar geen textbox in zit, in Display zit deze wel. Nu wil ik zelf dus een CustomItem maken en tevoorschijn laten komen in mijn Canvas zou anders niet weten hoe ik het anders mogelijk moet maken.
Dit is de code van mijn Canvas:
de customItem bestaat uit deze code:
wanneer ik q.paint(g, 100, 100); ergens in de paint() van de canvas gooi zie ik de customItem op mijn canvas flikkeren (zal wel een buffering probleem zijn welke ik ook nog moet oplossen). zodra ik hem buiten mijn paint() wil aanroepen (bijvoorbeeld in run() ) doet hij niks.
Kan iemand mij op weg helpen? ben maar een newbie op het gebied van java (is volgens mij wel duidelijk)
alvast bedankt!
Ik heb heb een canvas met sprites erin, wanneer je met het poppetje tegen een bepaalde sprite aanloopt wil ik dat je een textbox krijgt met daarin een vraag.
nu ben ik al de hele dag aan het zoeken hoe ik dat voor elkaar krijg...
Ik ben erachter gekomen dat canvas een low level api is en dat daar geen textbox in zit, in Display zit deze wel. Nu wil ik zelf dus een CustomItem maken en tevoorschijn laten komen in mijn Canvas zou anders niet weten hoe ik het anders mogelijk moet maken.
Dit is de code van mijn Canvas:
Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
| import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; import java.io.IOException; public class SnakeCanvas extends GameCanvas implements Runnable { private volatile boolean mTrucking; private SnakeSprite mSnake; //object snake declareren uit klassen SnakeSprite private FruitSprite mStrawberry; private FruitSprite mApple; private FruitSprite mQuestion; private TiledLayer mBoard; private LayerManager mLayerManager; private int score = 0; private TextBox textBox= null; private Question q; boolean question = false; public SnakeCanvas() throws IOException { super(true); mSnake = createSnake(); mSnake.setPosition(17,17); mBoard = createBoard(); mStrawberry = createStrawberry(); mStrawberry.setPosition((3*16),(2*16)); mApple = createApple(); mApple.setPosition((8*16), (8*16)); mQuestion = createQuestion(); mQuestion.setPosition((8*16), (2*16)); q = new Question("vraag", "1", "2"); mLayerManager = new LayerManager(); mLayerManager.append(mSnake); mLayerManager.append(mBoard); mLayerManager.append(mStrawberry); mLayerManager.append(mApple); mLayerManager.append(mQuestion); } private SnakeSprite createSnake() throws IOException { Image image = Image.createImage("snake3.png"); return new SnakeSprite(image, 16, 16); } private FruitSprite createStrawberry() throws IOException { Image image = Image.createImage("strawberry_16x16.png"); return new FruitSprite(image, 16, 16); } private FruitSprite createApple() throws IOException { Image image = Image.createImage("apple_16x16.png"); return new FruitSprite(image, 16, 16); } private FruitSprite createQuestion() throws IOException { Image image = Image.createImage("question.png"); return new FruitSprite(image, 16, 16); } private TiledLayer createBoard() throws IOException { Image image = Image.createImage("forest.png"); TiledLayer tiledLayer = new TiledLayer(10, 10, image, 16, 16); //10 kolommen en 10 rows plaatjes zijn 16x16 int[] map = { 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; for (int i = 0; i < map.length; i++) { int column = i % 10; int row = (i - column) / 10; tiledLayer.setCell(column, row, map[i]); } return tiledLayer; } public void start() { mTrucking = true; Thread t = new Thread(this); t.start(); } public void run() { Graphics g = getGraphics(); int timeStep = 80; while (mTrucking) { long start = System.currentTimeMillis(); tick(g); input(); score(g, score); render(g); long end = System.currentTimeMillis(); int duration = (int)(end - start); //debug(g, duration * 100 / timeStep); if (duration < timeStep) { try { Thread.sleep(timeStep - duration); } catch (InterruptedException ie) { stop(); } } } } private void tick(Graphics g) { //mState = (mState + 1) % 20;//tijdens de thread wordt het object X% vergroot en x keer wordt het figuur gedraaid if(mSnake.collidesWith(mBoard, true)) mSnake.undo(); if(mSnake.collidesWith(mStrawberry, true)){ mStrawberry.setVisible(false); score += 100; //System.out.println("score: "+score); } if(mSnake.collidesWith(mApple, true)){ mApple.setVisible(false); score += 150; } if(mSnake.collidesWith(mQuestion, true)){ mSnake.undo(); //getQuestion(); question = true; System.out.println("content height: "+q.getMinContentHeight()); q.paint(g, 100, 100); } } private void input() { int keyStates = getKeyStates(); if ((keyStates & LEFT_PRESSED) != 0) mSnake.turn(-1); else if ((keyStates & RIGHT_PRESSED) != 0) mSnake.turn(1); else if ((keyStates & UP_PRESSED) != 0) mSnake.forward(2); else if ((keyStates & DOWN_PRESSED) != 0) mSnake.forward(-2); } private void render(Graphics g) { int w = getWidth(); int h = getHeight(); g.setColor(0xffffff); g.fillRect(0, 0, w, h); int x = (w - 160) / 2; int y = (h - 160) / 2; //score(g, score); mLayerManager.paint(g, x, y); g.setColor(0x000000); g.drawRect(x, y, 160, 160); flushGraphics(); /*Any rendering on the returned Graphics object is done in an offscreen buffer. You can then copy the buffer to the screen using flushGraphics(), which does not return until the screen has been updated. This approach gives you finer control than calling repaint(). The repaint() method returns immeditately and your application has no guarantees about exactly when the system will call paint() to update the screen.*/ } private void debug(Graphics g, int percent) { int w = getWidth(); int h = getHeight(); StringBuffer sb = new StringBuffer(); sb.append(Integer.toString(percent)); sb.append('%'); String s = sb.toString(); Font font = g.getFont(); int sw = font.stringWidth(s) + 2; int sh = font.getHeight(); // Draw the render capacity. g.setColor(0xffffff); g.fillRect(w - sw, h - sh, sw, sh); g.setColor(0x000000); g.drawRect(w - sw, h - sh, sw, sh); g.drawString("" + percent + "%", w, h, Graphics.RIGHT | Graphics.BOTTOM); flushGraphics(); } private void score(Graphics g, int score){ int w = getWidth(); int h = getHeight(); String sScore = Integer.toString(score); g.drawString(""+sScore + "", w, h, Graphics.RIGHT | Graphics.BOTTOM); //q.paint(g, 100, 100); flushGraphics(); } public boolean getQuestion(){ System.out.println("return:" +question); return question; } public void stop() { mTrucking = false; } } |
de customItem bestaat uit deze code:
Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
| // // Question.java // Snake // // Created by Derk Dukker on 15-6-07. // Copyright 2007 __MyCompanyName__. All rights reserved. // import javax.microedition.lcdui.*; public class Question extends CustomItem implements ItemCommandListener { private final static int UPPER = 0; private final static int IN = 1; private final static int LOWER = 2; // status tells us where the cursor is on the form, UPPER, IN or LOWER private int status = UPPER; // location tells us where the cursor is inside the item private int location = 1; // checked tells which of the choices is selected private int checked=1; private String caption1; private String caption2; private final static Command CMD_EDIT = new Command("Edit", Command.ITEM, 1); public Question(String title, String caption1, String caption2) { super(title); this.caption1 = caption1; this.caption2 = caption2; setDefaultCommand(CMD_EDIT); setItemCommandListener(this); } protected int getMinContentHeight() { return 40; } protected int getMinContentWidth() { return 95; } protected int getPrefContentHeight(int width) { return 40; } protected int getPrefContentWidth(int height) { return 95; } protected void paint(Graphics g, int w, int h) { // First the outer rectangle g.setStrokeStyle(Graphics.DOTTED); g.drawRect(0, 0, 94, 39); g.setStrokeStyle(Graphics.SOLID); // Then the circles g.drawArc(4, 4, 13, 13, 0, 360); g.drawArc(4, 22, 13, 13, 0, 360); // Then draw the selected value if (checked==1) g.fillArc(4, 4, 13, 13, 0, 360); else g.fillArc(4, 22, 13, 13, 0, 360); // Then the 'cursor' if (location==1) g.drawRect(2, 2, 90, 17); else g.drawRect(2, 20, 90, 17); // And finally the texts g.drawString(caption1, 22, 4, Graphics.TOP|Graphics.LEFT); g.drawString(caption2, 22, 22, Graphics.TOP|Graphics.LEFT); //repaint(); } protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) { switch (dir) { case Canvas.DOWN: if (status == UPPER) { status = IN; } else { if (location==1) location=2; else if (location==2) return false; } repaint(); break; case Canvas.UP: if (status == LOWER) { status = IN; } else { if (location==2) location=1; else if (location==1) return false; } repaint(); break; } return true; } public void commandAction(Command c, Item i) { if (c == CMD_EDIT) { checked=location; repaint(); notifyStateChanged(); } } public int getState(){ return checked; } } |
wanneer ik q.paint(g, 100, 100); ergens in de paint() van de canvas gooi zie ik de customItem op mijn canvas flikkeren (zal wel een buffering probleem zijn welke ik ook nog moet oplossen). zodra ik hem buiten mijn paint() wil aanroepen (bijvoorbeeld in run() ) doet hij niks.
Kan iemand mij op weg helpen? ben maar een newbie op het gebied van java (is volgens mij wel duidelijk)
alvast bedankt!