collision - Error at GFX.java:37 -
i'm trying collision thought got apparently not when ran game got error.. annoying. i've come here :d :p anyway kept on getting error
exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ com.shiny21.graphics.gfx.step(gfx.java:37) @ com.shiny21.graphics.gfx.actionperformed(gfx.java:65) @ javax.swing.timer.fireactionperformed(unknown source) @ javax.swing.timer$dopostevent.run(unknown source) @ java.awt.event.invocationevent.dispatch(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
i don't understand errors yet heres code gfx:
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(); } }
so heres collision code:
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); } } }
edit 1:
i error
exception in thread "main" java.lang.nullpointerexception @ com.shiny21.framework.collision.<init>(collision.java:14) @ com.shiny21.graphics.gfx.<init>(gfx.java:25) @ com.shiny21.framework.game.main(game.java:18)
i added collision class:
blue = new playerb(blue.getx(), blue.gety()); red = new playerr(red.getx(), red.gety());
your field col
null , never set. line col.collisionstep();
Comments
Post a Comment