Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Java Threads to create an object that test collision detection
    primarykey
    data
    text
    <p>I am using the acm library(acm.jar), same library Stanford students used for their Java course CS106A to create graphical application easier.</p> <p>acm.jar can be found here: <a href="http://jtf.acm.org/" rel="nofollow">http://jtf.acm.org/</a></p> <p>The following code adds a character sprite to a graphics window. If I press z , the character animates itself moving his bow and arrows starts to spawn moving vertically through the use of Java Threads. There is no errors so far.</p> <p>Now I want to be able to able to perform collision detection on a threaded <code>GImage object(linkArrow)</code>. An error happened in my program when I tried doing this:</p> <pre><code>arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY()); arrowObject = getElementAt(arrowPoint); </code></pre> <p>I get this error:</p> <pre><code>Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException </code></pre> <p>You can ctrl+f and type bug to see the error location of my program. </p> <p>I have done collision detection before using graphical rectangles and oval using the <code>GPoint</code> and <code>getElementAt</code> for my game "BreakOut" and never had a problem using <code>GPoint</code> and <code>getElementAt</code>.</p> <p>Here is my code: first class is main program that runs the thread. The second class is the thread.</p> <pre><code> import java.awt.event.KeyEvent; import acm.graphics.GImage; import acm.program.GraphicsProgram; import acm.util.RandomGenerator; public class Link extends GraphicsProgram { private static final double GRAVITY = 1; public void init(){ setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT); addLink(); addKeyListeners(); } private void addLink(){ linkCharacter = new GImage("link sprites/linkFaceRight/link_frame_1_face_right.png"); add(linkCharacter,link_Location_XCoord,link_Location_YCoord); } public void keyPressed(KeyEvent e){ /* Link's Movement * */ char linkMoveRightKey = e.getKeyChar(); if(linkMoveRightKey == 'z') { xSpeed = 0; ySpeed = 0; pause(arrowDELAY); linkCharacter.move(xSpeed, ySpeed); linkCharacter.setImage(linkAttackWithBow[linkFrame]); linkFrame++; callArrow(); // BUG arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY()); arrowObject = getElementAt(arrowPoint); } // /* * summon link's arrow */ } if(linkFrame&gt;=linkAttackWithBow.length){ linkFrame = 0; } } private void callArrow(){ if(linkFrame == 2){ linkArrow = new ArrowThread(SIZE, rgen.nextColor()); add(linkArrow,linkCharacter.getX(),linkCharacter.getY()); Thread arrowThread = new Thread(linkArrow); arrowThread.start(); } } private ArrowThread linkArrow; private int SIZE = 400; private RandomGenerator rgen = RandomGenerator.getInstance(); private GImage gameBackgroundImage; private GImage linkCharacter; private double arrowDELAY = 28; private int link_Location_XCoord = 50; private int link_Location_YCoord = 50 ; private final int APPLICATION_WIDTH = 1200; private final int APPLICATION_HEIGHT = 800; private String[] linkAttackWithBow = {"link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png"}; private int linkFrame = 0; private int xSpeed =5; //the number of pixels to move in x private int ySpeed = 0; //0 so you only move horizontally } </code></pre> <p>Here is my Thread class to spawn the arrows</p> <pre><code>import java.awt.Color; import acm.graphics.GImage; public class ArrowThread extends GImage implements Runnable{ public ArrowThread(int SIZE,Color color){ super("link sprites/linkAttackWithBow/arrow.png", SIZE,SIZE); } public void run(){ for(int i = 0; i &lt; 1000/STEP;i++){ pause(60); move(arrowSpeedX,arrowSpeedY); } } private static final int arrowSpeedX = 0; private static final int arrowSpeedY = -5; private static final double STEP = 5; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload