Why would you de-encapsulate? I went straight to an “entity list” of bullets just to show why you shouldn’t.
http://pastebin.java-gaming.org/bd41013570b1b
EDIT: Crap, copied the un-fixed position.add(), just change it to use the .cpy()
Why would you de-encapsulate? I went straight to an “entity list” of bullets just to show why you shouldn’t.
http://pastebin.java-gaming.org/bd41013570b1b
EDIT: Crap, copied the un-fixed position.add(), just change it to use the .cpy()
Must have chose the wrong option in pastebin. The code is does exactly what mine did, except yours is worded differently. I just pasted it into eclipse and imported everything, still no movement.
Try removing the hasBeenSet check: (Also it’s probably a good idea to not use hardcoded values for buttons)
-if(Gdx.input.isButtonPressed(0) && !velocityHasBeenSet) {
+if(Gdx.input.isButtonPressed(Buttons.LEFT)) {
Otherwise I really don’t know why this wouldn’t work other than you haven’t made this the active screen.
Wow, removing the Boolean made it work, the speed is just really slow for now. Thanks for helping me out ;D
Increase BULLET_SPEED to make bullets faster. That’s what it’s there for.
I had to bring it up to 10k. Without removing that one part, it actually did work, but my button mechanics aren’t too good, and the speed was low, so It wasn’t noticeable.
Oh, the struggle when you don’t understand what your code really does. I would strongly suggest you to go over some online linear algebra tutorials about vectors, it will help a lot.
BurntPizza’s code is the good way of doing what you want but using that code the bullet still won’t stop at the cursor position (not sure why you need this feature though). If that’s what you want do what I suggested: Check the bullet’s velocity’s length and the distance between the bullet and it’s destination and if the distance is smaller than the velocity’s length set the bullet to be at the destination, stop the movement (maybe apply some condition to stop moving).
A small graph that I put together, hope it’s helps to understand the problem:
http://i.imgur.com/CCoxxes.png
(click for full res)
Okay, I’ll go read about vectors and linear algebra. Based off of your graph, I’m in need of the strength. Why I need it, I’m simply trying to make a bullet shoot towards the mouse like a shooting game.
Besides learning about Vectors, I think this was a pretty useless attempt. I was trying to achieve this,
http://www.gifs.net/Animation11/Everything_Else/Guns_and_Cannons/Cannon_shoots_3.gif
and I got a bullet teleporting half way at a speed of 29999. I don’t really see the movement so I might as well do something like this
Like the first gif, It was suposed to move towards the target at a steady enough pace to look a bit realistic.
This is simple use of vectors to simulate basic physics, specifically numeric integration of the equations of motion.
See more here: http://gafferongames.com/game-physics/integration-basics/
The only thing I’m having trouble with is making it shoot only when the mouse is pressed. I tried a lot of decisions and Booleans but am not getting it right.
I recommend something along the lines of ClickListener, which basically works the same as MouseListeners in Swing.
EDIT: Actually you probably just want InputProcessor because you don’t need scene2d stuff I’m guessing.
or if you don’t want to use ClickListener, you could do something like
boolean hasReleasedMouse = true;
public void shoot(){
if(Input.mousePressed){
if(hasReleasedMouse){
hasReleasedMouse = false;
//shoot
}
}else{
hasReleasedMouse = true;
}
}
Pretty much what I’m doing but I have it wait about a second and then it disposes the texture. How do i remove the texture without perminantly disposing it?
What do you mean “remove the texture”? Stop rendering it?
In the render method, I’m trying to click once and it shoots the bullet but it keeps on going because of how the render method is set up. I tried changing it so the bullet doesn’t keep going, but I’d have to dispose it.
if (Gdx.input.isButtonPressed(0) && !pressed){
entities.add(new Entity(cannonBall));
pressed = true;
b = true;
}
batch.begin();
for (Entity e : entities) {
e.update(delta);
//if(!pressed)
e.render(batch);
if(b)
try {
resetCanonball();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
batch.end();
}
void resetCanonball() throws InterruptedException {
if( frameCount++ == 10){
cannonBall.dispose();
frameCount = 0;
}
}
Don’t dispose the texture, it’s separate from all the bullet entities. You want to remove the bullet entity from the entities list, then it won’t get updated or rendered (and eventually garbage collected).
What is your criteria for removing a bullet? Leaving screen bounds? Time limit?
I was trying to do
entities.remove(cannonBall)
.
cannonBall isn’t an entity, it’s the texture. Maybe it should be renamed “cannonBallTexture”…
Again, what exactly is your removal criteria?
Ah, well instead of screen bounds, I’m aiming for it to jump from point A to B only when clicked.