2013-07-24 141 views
-5

嗯,我試圖解決這個問題,但我不能,如果有人能幫助我...類型不匹配:不能從int轉換爲Boolean

CODE:

package com.skybox.runordie; 

import java.awt.BorderLayout; 
import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 
import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferInt; 

import javax.swing.JFrame; 

public class Game extends Canvas implements Runnable { 

    public static final long serialVersionUID = 1L; 

    public static final int WIDTH = 160; 
    public static final int HEIGHT = WIDTH/12 * 9; 
    public static final int SCALE = 3; 
    public static final String NAME = "Run Or Die"; 

    private JFrame frame; 

    public boolean running = false; 
    public int tickCount = 0; 

    private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB); 
    private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); 


    public Game() { 
    setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
    setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 

    frame = new JFrame(NAME); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(new BorderLayout()); 

    frame.add(this, BorderLayout.CENTER); 
    frame.pack(); 

    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
    } 


    public synchronized void start() { 
    running = true; 
    new Thread(this).start(); 
    } 

    public synchronized void stop() { 
    running = false; 
    } 

    public void run() { 
    long lastTime = System.nanoTime(); 
    double nsPerTick = 1000000000D/60D; 

    int ticks = 0; 
    int frames = 0; 

    long lastTimer = System.currentTimeMillis(); 
    double delta = 0; 

    while(running) { 
     long now = System.nanoTime(); 
     delta += (now - lastTime)/nsPerTick; 
     lastTime = now; 
     boolean shouldRender = true; 

     while (delta >= 1) { 
     ticks++; 
     tick(); 
     delta -= 1; 
     shouldRender = true; 
     } 
     try { 
     Thread.sleep(2); 
     } catch (InterruptedException e) { 
     e.printStackTrace(); 
     } 
     if (shouldRender) { 
     frames++; 
     render(); 
     } 
     if(System.currentTimeMillis() - lastTimer > 1000) { 
     lastTimer += 1000; 
     System.out.println(frames + " frames," + ticks + " ticks."); 
     frames = 0; 
     ticks = 0; 
     } 
    } 
    } 

    public void tick() { 
    tickCount++; 
    for(int i = 0; pixels.length; i++) { 
     pixels[i] = i + tickCount; 
    } 
} 

    public void render() { 
    BufferStrategy bs = getBufferStrategy(); 
    if(bs == null) { 
     createBufferStrategy(3); 
     return; 
    } 

    Graphics g = bs.getDrawGraphics(); 
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 
    g.setColor(Color.BLACK); 
    g.fillRect(0, 0, getWidth(), getHeight()); 

    g.dispose(); 
    bs.show(); 
    } 

    public static void main(String[] args) { 
    new Game().start(); 
    } 


    public int[] getPixels() { 
    return pixels; 
    } 


    public void setPixels(int[] pixels) { 
    this.pixels = pixels; 
    } 

} 
+2

發佈您的代碼,以及語言添加到標籤。並告訴我們哪一行代碼會導致問題。 –

+0

請在此發佈*相關的*代碼... –

+0

問題的代碼太多,特別是沒有任何行號或給出的更多信息。 – jbabey

回答

5

在什麼你有錯誤嗎?如果我猜,這是在這裏:

for(int i = 0; pixels.length; i++) { 
    pixels[i] = i + tickCount; 
} 

for語句中的第二句話應爲循環,這是一個布爾值持續符合的條件。然而,pixels.length不是布爾值,而是整數。我假設你的意圖是該整數比較另一整,導致一個布爾值:

for(int i = 0; i < pixels.length; i++) { 
    pixels[i] = i + tickCount; 
} 

僅僅通過自身提供的整數不會告訴持續狀態的循環什麼。它需要知道繼續循環的條件(否則循環應該結束)。在這種情況下,該條件是當i小於pixels.length時。

+0

這應該直接在IDE上彈出......也就是說,如果OP正在使用一個。仍然,好,趕上。 – Gamb

+0

@Gamb所有構建錯誤「應該」會彈出在任何好的IDE上...... – MathSquared

+0

@Gamb,等:即使沒有IDE,編譯器本身也會指定錯誤和發現錯誤的行。 – David

0

的問題是在這裏下面的方法

public void tick() { 

      tickCount++; 

      for(int i = 0; pixels.length; i++) { // should compare pixels.length to some int to get boolean return 

        pixels[i] = i + tickCount; 

      } 
相關問題