Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Firing Bullets in certain direction  (Read 2517 times)
0 Members and 1 Guest are viewing this topic.
Offline hacker9116

Junior Newbie





« Posted 2012-03-05 15:25:24 »

hi i was just wondering if anybody can help me with my problem? if so great and thanks Smiley

i am currently working on my college project i am making a top down shooter game i have it coded so that when the player presses WASD keys the sprites image changes to face the current direction the problem i am running into is that i have variables that are tied to the the methods that change the image to different directions to tell what position is currently in so i know which was to fire the bullets.

the problem is when i fire the bullet it goes the way i want it to go but when i change direction using the keyboard the bullet goes in the same direction as the player is facing like it should but thats a problem.

is there a way to when the user fires the bullets it can no longer be control by the keyboard i.e it just fire the way its facing and when the player changes direction the bullet still travels that way.

Any help would be greatly appreciated thank you.
Offline Mike

« JGO Spiffy Duke »


Medals: 149
Projects: 1
Exp: 6 years


Java guru wannabe


« Reply #1 - Posted 2012-03-05 15:29:34 »

Easiest way is to create a class called bullet and pass it something like:
startX, startY, deltaX, deltaY (or just direction if you don't want to allow 360 degrees)

You can put the bullet object into a collection (or whatever you want to use to decide what should be drawn each frame) and then it won't change direction anymore Smiley

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline hacker9116

Junior Newbie





« Reply #2 - Posted 2012-03-05 15:46:39 »

code
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mike

« JGO Spiffy Duke »


Medals: 149
Projects: 1
Exp: 6 years


Java guru wannabe


« Reply #3 - Posted 2012-03-06 08:58:25 »

Give things good names, no need in keeping them really short (m, ms? meters and milliseconds? Tongue) Smiley

What is up with m.move(); I do see a function for it that doesn't do anything (but that has some parameters), so where is the real one inherited from? I see a moveLeft and a moveRight function and I guess they are called from move. What decides whether moveLeft or moveRight is called? mainPlayer.getDirectionX()? If so you should store the direction when the bullet is created and reuse that.

Both move left and move right has this btw:
      if (getPositionX() > BOARD_WIDTH)
         setVisible(false);
I am guessing moveLeft should have:
      if (getPositionX() < 0)
         setVisible(false);

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (160 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (901 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!