This site requires JavaScript, please enable it in your browser!
Greenfoot back
ksimuscb
ksimuscb wrote ...

2020/11/25

Deny CheckKeypress()/isKeyDown() and Force a move()

ksimuscb ksimuscb

2020/11/25

#
Note: I tried to provide as much information and clarity as possible while allowing for vague terminology to make the point more concise to comprehend. I'm working on a project for my class, and I need to be able to deny method CheckKeypress() or isKeyDown() and force a move() in the opposite direction. Example: 1 private void checkKeypress() 2 { 3 // speed =1; 4 if (Greenfoot.isKeyDown("a")) 5 { 6 setLocation(getX()-speed, getY()); 7 } // end if "to move left" 8 } // end method checkKeypress I'm implementing it so when it touches a certain object, the player character is force moved at 180 degrees for a certain amount of cycles to create a minimum amount of distance with that object. Example: 1 private void checkCollision() 2 { 3 if (isTouching(Threat.class)) 4 { 5 if (Greenfoot.isKeyDown("a")) 6 { 7 invertMove(); 8 } // end if KeyDown"a" 9 if (Greenfoot.isKeyDown("d")) 10 { 11 invertMove(); 12 } // end if KeyDown"d" 13 } // end if isTouching 14 } // end method checkCollision My problem exists in how to define a method to invert the movement while denying the keypress. This is the best I can manage on my own, as I'm still barely knowledgeable with utilizing Java. For the sake of brevity, imagine the code mirrors the "if (isMovingLeft == true)" for "if (isMovingLeft == false)". 1 private void invertMove() 2 { 3 if (isMovingLeft == true) 4 { 5 deltaX =4; 6 moveCycle = moveCycle++; 7 setLocation( getX() + deltaX, getY()); 8 9 if (moveCycle < 100) 10 { 11 isMovingLeft = !isMovingLeft; 12 deltaX = -deltaX; 13 } // end if moveCycle < 100 14 if (moveCycle == 100) 15 { 16 isMovingLeft = !isMovingLeft; 17 deltaX =0; 18 } // end if moveCycle reset 19 } // end if isMovingLeft 20 } // end method invertMove I understand that however the keypress is denied, will effect how I can change the code to have the system force move.
danpost danpost

2020/11/26

#
If I am not mistaken, you want your coding in the following fomat: if inverting, invert else move and check collision
You need to login to post a reply.