首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GFX.java:37出错

GFX.java:37出错
EN

Stack Overflow用户
提问于 2014-08-19 02:04:47
回答 1查看 42关注 0票数 0

我正在尝试做碰撞,我以为我得到了它,但显然没有,所以当我运行我的游戏时,我得到了一个错误。这真的很烦人。所以我来这里寻求一些帮助:D希望:P不管怎样,我一直收到这个错误

代码语言:javascript
复制
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.shiny21.graphics.GFX.step(GFX.java:37)
    at com.shiny21.graphics.GFX.actionPerformed(GFX.java:65)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我还不能真正理解错误,但下面是代码GFX:

代码语言:javascript
复制
    package com.shiny21.graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;

import com.shiny21.framework.Collision;
import com.shiny21.players.PlayerB;
import com.shiny21.players.PlayerR;

public class GFX extends JPanel implements ActionListener{

    PlayerR red;
    PlayerB blue;
    Collision col;

    public GFX(){

        red = new PlayerR(getX(), getY());
        blue = new PlayerB(getX(), getY());

        this.setFocusable(true);
        this.requestFocus();

        setBackground(Color.BLACK);

        Timer timer = new Timer(1000 / 60, this);
        timer.start();
    }

    public void step(){

        col.CollisionStep();
        red.step();
        blue.step();
        //System.out.println("");

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        //RED
        g.setColor(Color.RED);
        g.fillRect(red.getX(), red.getY(), 8, 8);

        g.setColor(Color.WHITE);
        g.drawRect(red.getX()+20 - red.getSightW(), red.getY()+20 - red.getSightH(), red.getSightW(), red.getSightH());

        //BLUE
        g.setColor(Color.BLUE);
        g.fillRect(blue.getX(), blue.getY(), 8, 8);

        g.setColor(Color.GREEN);
        g.drawRect(blue.getX()+20 - blue.getSightW(), blue.getY()+20 - blue.getSightH(), blue.getSightW(), blue.getSightH());

    }


    public void actionPerformed(ActionEvent ae) {
        step();
        repaint();
    }
}

这就是碰撞代码:

代码语言:javascript
复制
    package com.shiny21.framework;

import java.awt.Rectangle;

import com.shiny21.players.PlayerB;
import com.shiny21.players.PlayerR;

public class Collision {

    PlayerB blue;
    PlayerR red;

    public Collision(){
        CollisionStep();
    }

    public void CollisionStep(){
        Rectangle r1 = blue.getBounds();
        Rectangle r2 = red.getBounds();

        if(r1.intersects(r2)){
            blue.setDX(-1);
            red.setDX(1);
        }
    }
}

编辑1:

我现在得到了这个错误

代码语言:javascript
复制
Exception in thread "main" java.lang.NullPointerException
at com.shiny21.framework.Collision.<init>(Collision.java:14)
at com.shiny21.graphics.GFX.<init>(GFX.java:25)
at com.shiny21.framework.Game.main(Game.java:18)

然后我将这个添加到Collision类:

代码语言:javascript
复制
blue = new PlayerB(blue.getX(), blue.getY());
red = new PlayerR(red.getX(), red.getY());
EN

回答 1

Stack Overflow用户

发布于 2014-08-19 02:06:01

您的字段col为空,并且从未设置。这一行col.CollisionStep();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25369417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档