简体   繁体   English

J2ME 游戏线程

[英]J2ME game thread

I'm trying to make a game for J2ME something like the game Zuma, the structure of classes that you need to know is as follow :我正在尝试为 J2ME 制作一个类似于 Zuma 游戏的游戏,您需要了解的类结构如下:

-Level class which holds info about a level such as a vector of points (class I made called Point, the path that the balls will follow), vector of balls (class I made called Ball) to hold different balls object, speed (the speed of the balls in that specific level).. (There're more fields but nothing that you need to worry about) -Level 类,其中包含有关级别的信息,例如点向量(我创建的类称为 Point,球将遵循的路径)、球向量(我创建的类称为 Ball)以容纳不同的球对象、速度(该特定级别中球的速度..(有更多领域,但您无需担心)

-Frog class (just so you'd know it exists) if you're not familiar wit the game zuma it serves as a controllable turret that can shoot balls.). -Frog 类(只是为了让您知道它存在)如果您不熟悉 zuma 游戏,它可以用作可以射球的可控炮塔。)。 (No need to worry about the fields in that class) (无需担心该类中的字段)

-MyGameCanvas which obviously extends GameCanvas and implement runnable, holds instance of frog and a level (the currently played level).. (There're more fields but nothing that you need to worry about). -MyGameCanvas 显然扩展了 GameCanvas 并实现了可运行,保存了青蛙的实例和一个关卡(当前播放的关卡)..(有更多的字段,但你不需要担心)。 The run method serve as a listener for user input if user pressed right rotate the frog (essentially a sprite) by X amount if pressed left rotate by -X amount, pressed OK shoot a ball.如果用户按下右旋转青蛙(本质上是一个精灵)X 量,如果按下左旋转 -X 量,按下 OK 射球,则 run 方法用作用户输入的侦听器。 Beside taking care of input the thread calls a render method which renders what going on, on the screen and updates balls position from the balls vector (by using the current level instance) using the vector of points (from the level instance aswell), now the problem is that I wanted each level to have different speed, by speed I mean that balls will "roll" (=move) on screen slower, so I can do it with just using the speed value in the Thread.sleep method and increase the sleep time because the run method is taking input from user making the frog movements and input reaction to be slowed down aswell, I thought maybe doing each ball a seperate thread but thats not really good in my opinion because there'll be alot of threads + when I update each ball location on the screen I actually use the WHOLE ball vector from the level instance for things like if a ball need to gove backwards if there's a gap between the ball and the one behind him, or when to render the ball depending if the ball 除了处理输入之外,线程调用渲染方法,该方法渲染屏幕上正在发生的事情,并使用点向量(也来自关卡实例)从球向量(通过使用当前关卡实例)更新球位置,现在问题是我希望每个级别都有不同的速度,速度我的意思是球会在屏幕上“滚动”(=移动)更慢,所以我可以只使用 Thread.sleep 方法中的速度值并增加睡眠时间,因为 run 方法正在从用户那里获取输入,使青蛙运动和输入反应也减慢,我想也许可以将每个球做一个单独的线程,但在我看来这并不是很好,因为会有很多线程+ 当我更新屏幕上每个球的位置时,我实际上使用关卡实例中的整个球向量来处理诸如球和他身后的球之间是否有间隙的球是否需要向后移动,或者何时渲染球取决于球after him is already rendered and on the "track" (the points vector), so I don't really know how I should do it, any advices guidance would be highly appriciated !在他已经渲染并在“轨道”(点向量)上之后,所以我真的不知道我应该怎么做,任何建议的指导都会非常受欢迎! Thanks in advance and hopefully you could understand what I wrote.. Also I don't think I need to give code examples really because I don't wanna use what I want to CHANGE what I wrote so it wouldn't really help providing the run/render methods, all you need to know is the structure of the classes I gave you and the fields they have that I told you about.在此先感谢您,希望您能理解我写的内容。此外,我认为我真的不需要给出代码示例,因为我不想使用我想更改的内容来更改我写的内容,因此它不会真正帮助提供运行/渲染方法,你只需要知道我给你的类的结构以及我告诉你的它们拥有的字段。

Various developers use various methods for controlling the speed of sprite-movement.各种开发人员使用各种方法来控制精灵移动的速度。 JavaME is one of the many platforms that requires you to also consider different resolutions and CPU speed. JavaME 是需要您同时考虑不同分辨率和 CPU 速度的众多平台之一。 So it's never as simple as just incrementing the x value 1 pixel in each cycle of the main game loop.因此,它绝不会像在主游戏循环的每个循环中仅将 x 值增加 1 个像素那么简单。

Assuming 2 devices with different screen resolution, but otherwise running the same speed.假设 2 台设备具有不同的屏幕分辨率,但运行速度相同。 Incremending x value by 1 in the cycle of the game loop, would obviously result in the sprite reaching the end of the screen faster on the small resolution.在游戏循环的循环中将 x 值增加 1,显然会导致精灵在小分辨率下更快地到达屏幕末端。 Assuming 2 device with the same screen resolkution, but with different CPU's.假设 2 台设备具有相同的屏幕分辨率,但具有不同的 CPU。 Incrementing x value by 1 in the cycle of the game loop, would obviously result in the sprite reaching the end of the screen faster on the device with the fastest CPU.在游戏循环的循环中将 x 值增加 1,显然会导致精灵在具有最快 CPU 的设备上更快地到达屏幕末端。

Here is one way of working around that:这是解决此问题的一种方法:

Create a variable, call it itemWait (where "item" is the name of the object you want to control the speed for).创建一个变量,将其itemWait (其中“item”是您要控制其速度的对象的名称)。 The itemWait variable holds a value that tells the game loop how long should pass before next movement. itemWait变量保存一个值,该值告诉游戏循环在下一次移动之前应该经过多长时间。 For example, say that the sprite should wait 40 milliseconds for each pixel it moves.例如,假设精灵应该为它移动的每个像素等待 40 毫秒。 The code would look somewhat like this:代码看起来有点像这样:

// Variables defined outside the game loop
long lastTime, thisTime;
int cyclems;
int itemTime; // Replace "item" with the name of the object, like e.g. ballTime.
int itemWait = 40; // Wait 40 ms for each movement
int itemPixelsToMove = 1;

while (running == true) { // Game loop
 lastTime = thisTime; // Set lastTime to the time-stamp of the previous cycle
 thisTime = System.currentTimeMillis(); // Get the current time-stamp
 cyclems = (int) (thisTime - lastTime); // How long did it take to execute previous loop

 itemTime += cyclems; // How long has passed since last move
 while (itemTime > itemWait) { // If it's time to move
  itemTime -= itemWait;
  moveItem(itemPixelsToMove);
 }

 // Do other game logic

 // Draw everything

 // Handle input, unless you handle it with keyPressed()
}

Using that method, the code doesn't care how fast the device is.使用这种方法,代码并不关心设备的速度。 And if you want to port the game to a different screen resolution, you can simply change either itemPixelsToMove or itemWait .如果您想将游戏移植到不同的屏幕分辨率,您只需更改itemPixelsToMoveitemWait For example, say you set itemWait = 40;例如,假设您设置itemWait = 40; for 240x320 resolutions. 240x320 分辨率。 Then you would set it to 40/240*480 = 80 for 480x640 resolutions.然后,对于 480x640 分辨率,您可以将其设置为 40/240*480 = 80。 (This requires of course, that you also scale the graphics). (当然,这要求您还缩放图形)。

We've used this approach in www.PirateDiamonds.com - in case you're curious.我们在 www.PirateDiamonds.com 中使用了这种方法 - 以防您好奇。 The same code runs on whatever screen resolution you want.相同的代码可以在您想要的任何屏幕分辨率上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM