Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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  
  Executing user typed code  (Read 865 times)
0 Members and 1 Guest are viewing this topic.
Offline saywhatyousee

Junior Newbie





« Posted 2013-12-17 12:24:36 »

I am creating a learning aid in the scenario of a game which allows the users to type their own code into a JTextArea for it to be compiled and executed to traverse the player through the level.
Within the learning aid currently, I want to allow the user to use conditional statements and loops so that they can detect if the player has hit a wall while it was moving. An example code of this would look like this:

1  
2  
3  
4  
5  
6  
7  
8  
//The numbers passed into the methods are the number of tiles to move in that direction.
player.moveUp(2);
player.moveLeft(4);
while(player.isMoving()){
   if(player.isColliding()){
       player.moveRight(3);
   }
}


However, when I test this code out, the while() loop and the if statement returns true, but the player.moveRight(3); within the if statement does not get executed.

Within the player class what I am currently doing is storing the player's movements into an array and assigning target x and y coordinates for the player to move towards. The player will move in the direction specified for the amount passed in within the parameter. Once the player has reached the target distance, it will then move in the second direction that the user might have specified, until it has emptied the array containing the move directions.

I tried emptying the move direction array when the player has hit the wall, but that did not work either.

Here is the player class: http://www.java-gaming.org/?action=pastebin&id=777
Offline innoxium

Junior Newbie





« Reply #1 - Posted 2013-12-17 13:07:21 »

1  
2  
3  
4  
5  
 private boolean isMoving = false;
   
    public boolean isPlayerMoving(){
        return isMoving;
    }


isMoving never changes from false thus never hits the inside of loop

That and I see nowhere the following bit of code being ran:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
   public void updatePosition(){

       double targetX = playerMapPosision[0];
       double targetY = playerMapPosision[1];

       if(targetX > 0 && targetY > 0 && !commandsHopper.isEmpty() && pause == false){

           isMoving = true;
           
            if(y > targetY) {
               
                moveCharacter(x, y - moveSpeed);

            }else if(y < targetY) {

                 moveCharacter(x, y + moveSpeed);

            }else if(x > targetX) {

                 moveCharacter(x - moveSpeed, y);

            }else if(x < targetX) {
                 moveCharacter(x + moveSpeed, y);
            }

            if(y <= targetY && y >= targetY && x >= targetX && x <= targetX && playerReachedEnd == false || isColliding == true){
                commandsHopper.remove(0);
                getNextTarget();
            }
       }else{
            isMoving = false;
       }

    }


EDIT: Added second block of code, never see that method called.

Also, just woke up... heh
Offline saywhatyousee

Junior Newbie





« Reply #2 - Posted 2013-12-17 13:24:34 »

Updateposition(); is getting called in the update() method underneath it. The update(); method itself is being called in the main game loop so it is getting called numerous times a second.

Also, is moving is set to true within the updateposition(); method once an initial target x, and y coordinates have been set. This is checked by the first if statement in that method. Also even if I add isMoving = true; within all the move methods, the if statement within the while loop from my example code on the top post does not execute the player.moveRight(); method.

Thanks for the reply.

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

Junior Newbie





« Reply #3 - Posted 2013-12-17 13:29:18 »

Does it even reach that if?
Offline saywhatyousee

Junior Newbie





« Reply #4 - Posted 2013-12-17 13:34:52 »

Ah sorry for that. I just typed the example as an example, I was not using the actual method names which I have in the player class. I use isColliding(); to detect if the player has hit the wall.

The isColliding variable is set to true within the player class's parent class. It checks if the bounding box is colliding with the background and sets isColliding to true when it does. I then return isColliding within the isColliding(); method to allow the player to detect if the player is colliding with the wall.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (12 views)
2014-11-28 16:25:35

PocketCrafter7 (8 views)
2014-11-28 16:25:09

PocketCrafter7 (8 views)
2014-11-28 16:24:29

toopeicgaming1999 (75 views)
2014-11-26 15:22:04

toopeicgaming1999 (65 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!