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

2012/7/28

problems with getOneObjectAtOffset, and text

1
2
sparrow72 sparrow72

2012/7/28

#
so I have been making a tanks game for a while now and have a few questions about getOneObjectAtOffset, and text. here is the code for my intersection of bricks
public void ghost()
    {
        Actor collided = getOneObjectAtOffset(getX(),getY()+0,Brick.class);
        if(collided != null)
        {  
            setLocation(getX(),getY()-10);  //it moves in increments of 10
        }
     }
the problem is that it randomly decides to setLocation. I have also noted out setLocation and made it move(100); and it would go through some bricks and not others and then randomly it would (where no bricks are located at) move the 100. the second problem is not so much a problem as repeat coding. I have an actor called "Levels", a subclass of that "Level1", and a subclass of that "Ready1". my problem is that when "Levels" creates the text, the text is also applied to ALL of its subclass's. so how do I basically say in code to not repeat the code of the actor it extends. and by the way the reason I have it as a subclass is for organization because I plan to have multiple 'Levels'. anyway here is the game thanks
erdelf erdelf

2012/7/28

#
could you add the source to your game`?
sparrow72 sparrow72

2012/7/28

#
ya, i also kinda fixed the first problem, I used
if (!getIntersectingObjects(Brick.class).isEmpty())   
        {  
            setLocation(getX(),getY()-5); 
        }
but the second problem is still in there
danpost danpost

2012/7/28

#
For the second problem, all you need do is in the constructors of all the immediate sub-classes of 'Levels', add the line
String text = "";
For the first problem, you are having a 'barrier' issue. It matters not whether you are using 'setLocation' or 'move'. The problem stems from moving distances greater than the width and/or height of the object barriers. And, by the way, it is NOT random. What I prefer to do is create a my own 'travel' method (and call 'move' multiple times within it, as follows:
private void travel(int howFar)
{
    for (int i = 0; i < howFar; i++)
    {
        move(1);
        if (!getIntersectingObjects(Brick.class).isEmpty())
        {
            move(-1);
            break;
        }
    }
}
See my Barriers and Bars scenario for sample code.
sparrow72 sparrow72

2012/7/28

#
THANKS!! XD
sparrow72 sparrow72

2012/8/2

#
ok so I tested the barriers and bars scenario and your solution, the one problem I see is that I turn and you have it just move up, down, left, and right, and if I have it turned moving -1 might just go back more into the brick. so for some time now I have been trying to get it to work. I thought I fianally came up with the solution put then I got this error
java.lang.NullPointerException
	at Brick.push(Brick.java:41)
	at Brick.act(Brick.java:32)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
here is the code to 'push', an method called inside of 'act'
public void push() 
    {
        int dx = getX();
        int dy = getY();
        if (!getIntersectingObjects(Tank.class).isEmpty())
        {
            if (tank.getX() <= dx)//less than or =
            {
                tank.setLocation(tank.getX(),tank.getY()-9);
            }
            if (tank.getX() >= dx)//more than or =
            {
                tank.setLocation(tank.getX(),tank.getY()+9);
            }
            if (tank.getY() <= dy)//less than or =
            {
                tank.setLocation(tank.getX()-9,tank.getY());
            }
            if (tank.getY() >= dy)//more than or =
            {
                tank.setLocation(tank.getX()+9,tank.getY());
            }
        }   
}
if you could help me understand the error that would be great. on a second issue I have found myself with the same second error as I had (it never was corrected) I have inserted the line
String text = "Level 1"; //and 
String text = ""; //(separately) 
in every single constructor (separately & simultaneously) and I still get the overlapping word problems. here is the current version of the code
SPower SPower

2012/8/2

#
A null pointer exception: java.lang.NullPointerException occurs when you use an object which is null. It happens at line 41 in the Brick class     at Brick.push(Brick.java:41)   and as far as I can see, it could happen here:
if (tank.getX() <= dx)
because tank might be null. What you must do now is click on this line in the error:     at Brick.push(Brick.java:41 And then, you see where it all happens.
sparrow72 sparrow72

2012/8/2

#
thanks, although the line you gave me is Brick.push(Brick.java:41). In addition it can't be null, in theory, because it can only be activated if it is in intersection, right? therefor how is it null when it was not activated?
danpost danpost

2012/8/2

#
The problem is you declare 'tank' in the Brick class, but never set it to anything (therefore it is 'null'). For the overlapping words, change the constructors in 'Levels' and 'Level1' so that the background image is set to 'new GreenfootImage("invisoButton.png")'. Also, the 'setImage' statement is unneccessary as the call to 'updateImage' will take care of it. EDIT: to be consistent, you probably want to do the same in 'Settings', 'Credits', and 'Controls', also.
sparrow72 sparrow72

2012/8/2

#
ok so I get your point and took out the line
private Tank tank;
but I then have another error, in the line:
            if (tank.getX() <= dx)//less than or =
it gives the error:
 cannot find symbol - variable tank
I get what it is saying but what I am trying to do is to get the tank's x position. so how would I go about doing this?
danpost danpost

2012/8/2

#
Once you are inside that code block, you know you are intersecting a Tank object. Insert as your first line in that block
Tank tank = (Tank) getOneIntersectingObject(Tank.class);
Reading this line backward, you get: get a Tank.class object that intersects 'this' (understood) and cast it to Tank; then, assign it to the variable 'tank' that is declared to hold a Tank object.
sparrow72 sparrow72

2012/8/2

#
ok so I did that and it worked, but... well see for yourself. I want it to kinda act like it does when it hits the edge of the world and slid on it too. here is the code that isn't working right.
 if (!getIntersectingObjects(Tank.class).isEmpty())
        {
            Tank tank = (Tank) getOneIntersectingObject(Tank.class);  
            int dx = getX();
            int dy = getY();
            if (tank.getX() <= dx)//less than or =
            {
                tank.setLocation(tank.getX(),tank.getY()-9);
            }
            if (tank.getX() >= dx)//more than or =
            {
                tank.setLocation(tank.getX(),tank.getY()+9);
            }
            if (tank.getY() <= dy)//less than or =
            {
                tank.setLocation(tank.getX()-9,tank.getY());
            }
            if (tank.getY() >= dy)//more than or =
            {
                tank.setLocation(tank.getX()+9,tank.getY());
            }
danpost danpost

2012/8/2

#
Something to consider: by having the 'push' in the Brick.class code, you are leaving a 'loophole' for one of the tanks. That is, if both tanks are hitting the same brick object from opposite sides, one will be allowed to slip through as only one of the tanks will be pushed. Therefore, "tank vs. brick" should be dealt with in the Tank.class code. It also makes more sense to have it there, being it deals with the location of the tank (not the brick). Each time tank object moves, check for brick collision and edge of world. In fact, do it multiple times each act (once for each pixel of each move horizontally and vertically). Upon brick intersection or edge contact, reverse that last one pixel movement.
sparrow72 sparrow72

2012/8/2

#
I have done this before. and it would work but what I am trying to achieve is a wall that a tank can slide on (like on the edge). I did find out one obvious error I had made in the code(I changed the x pos in the one that detected y pos and vise versa). but I still have the error of almost teleporting in the code:
if (tank.getY() <= dy)//less than or =
            {
                tank.setLocation(getX(),getY()-28);
            }
            if (tank.getY() >= dy)//more than or =
            {
                tank.setLocation(getX(),getY()+28);
            }
here is my updated scenario
sparrow72 sparrow72

2012/8/2

#
ok so I have another problem I am trying to make a actor called 'Top' that always is rotated to the x and y pos of the 'Tank' so I have the line
Tank tank = (Tank) getLocation(Tank.class); 
            turnTowards(tank.getX(),tank.getY());
this gives the error of java.lang.NullPointerException at Top.act(Top.java:25) the second line I gave is line 25
There are more replies on the next page.
1
2