我想从//create textures部分创建方法来缩短run(),但是如果我这样做并将来自//import部分的图像作为参数传递,那么就调用它如下:
createTextures(texture, texture2,ballTextureImport, greenFlashImport, blueFlashImport);
drawGraphics看不到它们的方法。
drawGraphics()只是重复add(everyParameter);
下面是run()内部代码的一部分
    // import
    Image texture = getImage(getCodeBase(), "texture.png");
    Image texture2 = getImage(getCodeBase(), "texture2.png");
    Image ballTextureImport = getImage(getCodeBase(), "ballTexture.png");
    Image greenFlashImport = getImage(getCodeBase(), "greenFlash.png");
    Image blueFlashImport = getImage(getCodeBase(), "blueFlash.png");
    // create textures
    GImage paddleLeftTexture = new GImage(texture);
    GImage paddleRightTexture = new GImage(texture2);
    GImage ballTexture = new GImage(ballTextureImport);
    GImage greenFlash = new GImage(greenFlashImport, -250, 0);
    GImage blueFlash = new GImage(blueFlashImport, -250, 0);
    paddleLeftTexture.setSize(WIDTH + 1, HEIGHT + 1);
    paddleRightTexture.setSize(WIDTH + 1, HEIGHT + 1);
    ballTexture.setSize(BALL_SIZE, BALL_SIZE);
    greenFlash.setSize(100, 300);
    blueFlash.setSize(100, 300);
    // make objects
    GOval ball = makeBall();
    GRect paddleLeft = makePaddle();
    GRect paddleRight = makePaddle();
    drawGraphics(ball, paddleLeftTexture, paddleRightTexture, ballTexture,
            greenFlash, blueFlash, counter, paddleLeft, paddleRight,
            aiScore, playerScore);来自drawGraphics()的其余参数是在run()和//make objects中前面创建的,看起来很好(没有用红色下划线)。
发布于 2014-04-05 09:26:51
我找到的解决方案是创建一个GImage类型的方法,而不是void,然后逐个创建纹理。
    private GImage createTexture(String importedImage, int width, int height) {
    Image importResult = getImage(getCodeBase(), importedImage);
    GImage textureResult = new GImage(importResult);
    textureResult.setSize(width, height);
    return textureResult;
}然后,在run()方法中
// make objects
    GImage paddleLeftTexture = createTexture("texture.png", WIDTH + 1,
            HEIGHT + 1);
    GImage paddleRightTexture = createTexture("texture2.png", WIDTH + 1,
            HEIGHT + 1);
    GImage ballTexture = createTexture("ballTexture.png", (int) BALL_SIZE,
            (int) BALL_SIZE);
    GImage greenFlash = createTexture("greenFlash.png", 100, 300);
    GImage blueFlash = createTexture("blueFlash.png", 100, 300);
    GOval ball = makeBall();
    GRect paddleLeft = makePaddle();
    GRect paddleRight = makePaddle();
    greenFlash.setLocation(-200, 0);
    blueFlash.setLocation(-200, 0);
    // generate GUI
    drawGraphics(ball, paddleLeftTexture, paddleRightTexture, ballTexture,
            greenFlash, blueFlash, counter, paddleLeft, paddleRight,
            aiScore, playerScore);https://stackoverflow.com/questions/22874109
复制相似问题