我是一个Scala新手,并试图提高我的技能。我已经决定玩它,并努力做一个转换空间入侵者游戏提供的LWJGL。它由10个Java类组成,我已经从Eclipse加载、编译并成功地运行了这些类。
我的第一个Scala步骤是用一个Sprite.java类替换SpriteNew.scala类。我已经完成了第一关(见下文)。然而,我有几个问题:
任何反馈,教练,指导,建议,你可以在这方面给我极大的感谢。
为了简洁起见,我已经删除了原始Sprite.java类中的注释(如果您希望看到Sprite.java的原始未经修改的源代码,请转到这里 ):
package org.lwjgl.examples.spaceinvaders;
import java.io.IOException;
import static org.lwjgl.opengl.GL11.*;
public class Sprite {
private Texture texture;
private int width;
private int height;
public Sprite(TextureLoader loader, String ref) {
try {
texture = loader.getTexture(ref);
width = texture.getImageWidth();
height = texture.getImageHeight();
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(-1);
}
}
public int getWidth() {
return texture.getImageWidth();
}
public int getHeight() {
return texture.getImageHeight();
}
public void draw(int x, int y) {
glPushMatrix();
texture.bind();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, texture.getHeight());
glVertex2f(0, height);
glTexCoord2f(texture.getWidth(), texture.getHeight());
glVertex2f(width, height);
glTexCoord2f(texture.getWidth(), 0);
glVertex2f(width, 0);
}
glEnd();
glPopMatrix();
}
}这里是我第一次使用Scala版本作为一个工厂(使用对象和类):
package org.lwjgl.examples.spaceinvaders
import java.io.IOException
import org.lwjgl.opengl.GL11._
object SpriteNew {
def create (textureLoader: TextureLoader, resourceName: String): SpriteNew = {
def load: Texture = {
var texture: Texture = null
try {
texture = textureLoader.getTexture(resourceName)
}
catch
{
case ioe: IOException => {
println(ioe.getStackTraceString)
System.exit(-1)
}
}
texture
}
val texture = load
new SpriteNew(texture)
}
}
class SpriteNew (val texture: Texture) {
val width = texture.getImageWidth()
val height = texture.getImageHeight()
def draw(x: Int, y: Int) {
glPushMatrix
texture.bind()
glTranslatef(x, y, 0)
glBegin(GL_QUADS)
glTexCoord2f(0, 0)
glVertex2f(0, 0)
glTexCoord2f(0, texture.getHeight())
glVertex2f(0, height)
glTexCoord2f(texture.getWidth(), texture.getHeight())
glVertex2f(width, height);
glTexCoord2f(texture.getWidth(), 0)
glVertex2f(width, 0)
glEnd
glPopMatrix
}
}关于不能将构造函数设置为私有的问题1,我是不正确的。将关键字“私有”放在Scala类名后面和构造函数参数列表之前,使类在构造函数变为私有时保持公共状态。换句话说,“类SpriteNew (val纹理:纹理)”上面的片段将被更改为"class SpriteNew私有(val纹理:纹理)“{。
发布于 2011-11-24 04:12:21
问题1:你自己回答了。
问题2: Scala中的try / catch是一个表达式。而且,sys.exit是Nothing类型,它是底部类型,是其他所有类型的子类型,因此它不会放弃类型推断。所以您的create方法可以是
def create (textureLoader: TextureLoader, resourceName: String) = new SpriteNew (
try textureLoader.getTexture(resourceName)
catch { case ioe: IOException =>
println(ioe.getStackTraceString)
sys.exit(-1)
}
)使用apply作为创建方法名(而不是create)也更为常见,这样您就可以使用SpriteNew(a, b)创建一个新实例。
我喜欢在我的布局中避免浪费垂直空间。如果你是按代码行付费的话,那就不太好了,但是如果代码不是全部分散开来,你就更容易看到发生了什么,而且你可以在页面上放得更多,这样你就不会来回滚动。
https://codereview.stackexchange.com/questions/6208
复制相似问题