I added a thing to my game, so that when you press space you'll get a small speed boost. However, it only works correctly when you use the WASD keys. If you press the up and left or down and right arrowkeys, the boost won't work, but otherwise it does. Can anyone explain why it does that?
Here's the code:
boostTimer += Gdx.graphics.getDeltaTime();
if(boostTimer > 3){
snailBoost = 0;
boostTimer = 0;
}
//movement
if(Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A)){
if(Gdx.input.isKeyPressed(Keys.SPACE) && snailBoost < 20){
snailBoost++;
snail.getBounds().x -= (snailSpeed + 100) * delta;
}
//flips the sprite
snail.getSprite().setFlip(false, true);
snail.getBounds().x -= snailSpeed * delta;
//handles what happens when you go outside the game world
if(snail.getBounds().x < -100)snail.getBounds().x = 1920;
}
if(Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D)){
if(Gdx.input.isKeyPressed(Keys.SPACE) && snailBoost < 20){
snailBoost++;
snail.getBounds().x += (snailSpeed + 100) * delta;
}
snail.getSprite().setFlip(true, true);
snail.getBounds().x += snailSpeed * delta;
if(snail.getBounds().x > 2020)snail.getBounds().x = -100;
}
if(Gdx.input.isKeyPressed(Keys.UP) || Gdx.input.isKeyPressed(Keys.W)){
if(Gdx.input.isKeyPressed(Keys.SPACE) && snailBoost < 20){
snailBoost++;
snail.getBounds().y -= (snailSpeed + 100) * delta;
}
snail.getBounds().y -= snailSpeed * delta;
if(snail.getBounds().y < -100)snail.getBounds().y = 1080;
}
if(Gdx.input.isKeyPressed(Keys.DOWN) || Gdx.input.isKeyPressed(Keys.S)){
if(Gdx.input.isKeyPressed(Keys.SPACE) && snailBoost < 20){
snailBoost++;
snail.getBounds().y += (snailSpeed + 100) * delta;
}
snail.getBounds().y += snailSpeed * delta;
if(snail.getBounds().y > 1080)snail.getBounds().y = -100;
}
Sorry for messy code. :P
Still new to game development.
Cheap keyboards share the control lines of the keys, so some key combinations won't register correctly and trying to press more than 3-4 keys at once will result in nothing happening.
Either buy a more expensive keyboard, or choose keys so they work (such as the famous WASD you've already noticed works). Nothing to do with code.
Related
I am writing a small game in java and an obvious key component is being able to move the character with the arrow keys. However I cannot get all directions to work, and only the last two if statements of the following block actually activate.
public void Update(){
if(Canvas.keyboardKeyState(KeyEvent.VK_UP))
yVel = -1;
else
yVel = 0;
if(Canvas.keyboardKeyState(KeyEvent.VK_LEFT))
xVel = -1;
else
xVel = 0;
if(Canvas.keyboardKeyState(KeyEvent.VK_DOWN))
yVel = 1;
else
yVel = 0;
if(Canvas.keyboardKeyState(KeyEvent.VK_RIGHT))
xVel = 1;
else
xVel = 0;
x += xVel;
y += yVel;
}
My inputs are good, all keys are registering, but no math is taking place. If anyone has suggestions or a library/package that would make this easier, please let me know.
You override the values in the else blocks. You maybe want to do this.
public void Update(){
xVel = 0;
yVel = 0;
if(Canvas.keyboardKeyState(KeyEvent.VK_UP)) {
yVel -= 1;
}
if(Canvas.keyboardKeyState(KeyEvent.VK_LEFT)) {
xVel -= 1;
}
if(Canvas.keyboardKeyState(KeyEvent.VK_DOWN)) {
yVel += 1;
}
if(Canvas.keyboardKeyState(KeyEvent.VK_RIGHT)) {
xVel += 1;
}
x += xVel;
y += yVel;
}
Its very obvious. Everytime your loop gets executed it happens something like this:
If it executes the first if statement, it also executes the rest of the else statements.
it should be something like this:
xVal = 0;
yVal = 0;
if(keyDown){
yVal++;
}
else if(keyUp){
yVal--;
}
else if(keyLeft){
xVal--;
}
else{
xVal++;
}
//Your rest of the code.
In the lerping function below, it will slow down a camera which is following a moving object, and as the moving object slows down or stops the camera catches up to it. I need the opposite. I need the camera to move ahead of the moving object and slow down back to the object when the object slows/stops. I can't seem to make it work. Any ideas?
//this is the opposite of what I want.
float lerp = 0.1f;
Vector3 position = this.getCamera().position;
position.x += (Obj.x - position.x) * lerp;
position.y += (Obj.y - position.y) * lerp;
just increase to 110% instead of decreasing to 10%
float lerp = 1.1f;
position.x += (Obj.x + position.x) * lerp;
position.y += (Obj.y + position.y) * lerp;
Example:
public static void main(final String[] args)
{
double position = 0;
for (int i = 0; i < 100; i++)
{
position = (position + 10) * 1.1;
System.out.println("position = " + position);
}
}
I'm having troubles having my character stop once it hits the edges of the windows.
Here's my update method.
public void update(GameContainer gc, StateBasedGame sbg, int delta)
{
Input input = gc.getInput();
playerX += VelocityX;
gc.setShowFPS(Splash.showFps);
if(input.isKeyPressed(Input.KEY_F1))
{
Splash.showFps = !Splash.showFps;
}
if (input.isKeyDown(Input.KEY_RIGHT))
VelocityX = 10;
else if (input.isKeyDown(Input.KEY_LEFT))
VelocityX = -10;
else if (playerX >= 700)
VelocityX = 0;
else
{
VelocityX = 0;
}
}
I realize going off to left is occurring because I haven't coded it yet but the character goes off the screen the right
if (input.isKeyDown(Input.KEY_RIGHT)){
VelocityX = 10;}
else if (input.isKeyDown(Input.KEY_LEFT)){
VelocityX = -10;}
else{VelocityX = 0;}
if (playerX >699){
playerX=699;
VelocityX = 0;}
else if(playerX<1){
playerX=1;VelocityX = 0;
}
Problem fixed. Within the key detection you set the velocity to 0.
So instead
if (input.isKeyDown(Input.KEY_RIGHT))
VelocityX = 10;
else if (playerX >= 700)
VelocityX = 0;
Do something like
if (input.isKeyDown(Input.KEY_RIGHT))
{
VelocityX = 10;
if (playerX >= 700)
VelocityX = 0;
}
There are a couple things wrong I noticed. The first is your update issue: you want to player to move by their velocity. You also need to do your bounds checking. Another problem you have, that others haven't noted on, is that you have a right key preference, That means that if you are holding the right and left key, the player moves right. This is due to the if / else if statements. You should separate your ifs out for a more granular level of control:
public void update(GameContainer gc, StateBasedGame sbg, int delta)
{
Input input = gc.getInput();
//playerX += VelocityX; -> this is moved
gc.setShowFPS(Splash.showFps);
if(input.isKeyPressed(Input.KEY_F1))
{
Splash.showFps = !Splash.showFps;
}
VelocityX = 0;
if (input.isKeyDown(Input.KEY_RIGHT))
VelocityX += 10;
//removed the else
if (input.isKeyDown(Input.KEY_LEFT))
VelocityX -= 10;
//you want to bounds check regardless of the above statments
//get rid of the else
if ((playerX >= 700 && VelocityX > 0) || (playerX <= 0 && VelocityX < 0)) //check both sides
VelocityX = 0;
//move the player
playerX += VelocityX;
}
EDIT: Modified my code to fix the issue of being unable to move when at a boundary.
I currently have a program with a grid and lines that will be drawn between dots in the grid. It uses standard draw. I wish to limit the length of the lines, so that they can only go from the adjacent points, but do not know how I would do this.
Thanks
StdDraw.setCanvasSize(400, 400);
StdDraw.setXscale(0, 10);
StdDraw.setYscale(0, 10);
//dots
double radius = .15;
double spacing = 2.0;
for (int i = 0; i <= 4; i++) {
for (int j = 0; j <= 4; j++) {
StdDraw.setPenColor(StdDraw.GRAY);
StdDraw.filledCircle(i * spacing, j * spacing, radius );
}
}
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.text(0, 9.5, player1_name);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.text(5, 9.5, player2_name);
int turn = 1;
for (int i = 0; i <= 40; i++) {
if (turn % 2 == 0)
StdDraw.setPenColor(StdDraw.RED);
else
StdDraw.setPenColor(StdDraw.BLUE);
while(!StdDraw.mousePressed()) { }
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
System.out.println(x + " " + y);
StdDraw.setPenRadius(.01);
StdDraw.show(200);
while(!StdDraw.mousePressed()) { }
double x2 = StdDraw.mouseX();
double y2 = StdDraw.mouseY();
StdDraw.show(200);
double xround = Math.round(x);
double yround = Math.round(y);
double x2round = Math.round(x2);
double y2round = Math.round(y2);
int xroundb = (int) xround;
int yroundb = (int) yround;
int x2roundb = (int) x2round;
int y2roundb = (int) y2round;
StdDraw.line(xround, yround, x2round, y2round);
System.out.println("Line Drawn");
StdDraw.show();
Ah I get it. You are not asking about the actual line method which does work correctly, you want logic such that line is not called if adjacent dots are not selected.
Well, first we need to know which adjacent connections are allowed. That is can we have Vertical? Horizontal? Diagonal? I will explain each just in case
So you have spacing = 2.0. Well, that should be sufficient to check for adjacency.
if (Math.abs(x2round - xround) > spacing) {
// don't draw
} else if (Math.abs(y2round - yround) > spacing)) {
// don't draw
} else if (Math.abs(y2round - yround) > 0.0) && Math.abs(x2round - xround) > 0.0) {
// don't draw if diagonal connections are forbidden
// if diagonal is allowed, remove this else if condition
} else {
StdDraw.line(xround, yround, x2round, y2round);
}
So if you don't draw, then you have to enforce your game logic. Perhaps the player forfeits a turn. Perhaps the player is given another chance to select adjacent dots. It is up to you. It is always a bit crazy comparing doubles due to roundoff, so instead of using 0.0, you might want to choose a very small epsilon double value to make sure you catch all cases.
Myself shweta dodiya.I am having an problem related to sensor and logic in j2me which i had implemented for achieving the result in my project.The logic which i had implemented is as follows:-
sensor= (SensorConnection) Connector.open("sensor:acceleration");
try {
data = sensor.getData(1);
} catch (IOException ex) {
ex.printStackTrace();
}
for (int i = 0; i < data.length - 1; i++) {
value[i] = data[i].getDoubleValues()[0];
}
CurrentValX = value[0];//X-axis of sensor
CurrentValY = value[1];//y-axis of sensor
if (CurrentValX < PreValueX1) {
left = false;
right = true;
} else if (CurrentValX > PreValueX1) {
left = true;
right = false;
}
if (CurrentValY < PreValueY1) {
down = false;
up = true;
} else if (CurrentValY > PreValueY1) {
down = true;
up = false;
}
if (right == true && ballX < Scrwidth - 15) {
ballX += 4;
} else if (left == true && ballX > 15) {
ballX -= 4;
}
if (down == true && ballY < Scrheight - 15) {
ballY += 4;
} else if (up == true && ballY > 15) {
ballY -= 4;
}
CurrentValY = PreValueY1;
CurrentValX = PreValueX1;
//check for the collision of ball with the other object like brick
if (bricksprite.collidesWith(ballSprite, true)) {
if (right) {
ballX -= 10;
}
if (left) {
ballX += 10;
}
if (up) {
ballY += 10;
}
if (down) {
ballX -= 10;
}
}
The problem i am having is in the collision of the ball and the bricks.when it get collide i want to move ball in the opposite direction of the collision.But sometime the ball instead of moving in opposite direction it keep moving in same direction.I had getting the direction of the ball through the boolean up,down,left,right.
Please help me to solve it and guide me and correct me if i m wrong somewhere
Thanks in advance
I haven't read your code , But in past I have coded the same kinda game,
In that I have applied following logic;
1.when ball colides suppose consider ball is moving from right to left than collides at left wall at some angel. here your x and y was decreasing in uniform manner upon colission your x should increase while y should be decreasing with the same fashion.
2.when ball colides vertical wall , y should invert x should be same..
I hope it clears up the logic.