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

2012/6/29

Skipping a line?

1
2
erdelf erdelf

2012/6/29

#
I have the following code, and it happens that not every target is shot at the end.
           
addObject(target, spawnX, spawnY);
sniper.shoot();
spawnX and spawnY are random integers. I defined target and sniper earlier shoot is a method to spawn a bullet.
SPower SPower

2012/6/29

#
You maybe can set a breakpoint in your code, but if you really want help from this site, I think you need to post some more code or explain it better. What does shoot do? Why do you add the target to the world while you call shoot on sniper?
erdelf erdelf

2012/6/29

#
ok, I thought that would be enough. So, shoot() spawns a bullet with the direction on the target. The sniper should shoot on the target, because I make a cheat mode for my game.
danpost danpost

2012/6/29

#
It may be possible that two shots are directed at the same target (especially, if you are using getWorld().getObjects(Target.class).get(0) in the 'shoot' method). Try adding 'target' as a parameter in the 'shoot' method, and direct the shot to that particular target.
erdelf erdelf

2012/6/29

#
well, I use the turnTowards(x,y) method for the bullet. The world gives the coordinates with an other method which asks after the coordinates of the target
danpost danpost

2012/6/29

#
I still think you should either try what I had suggested above, or, instead of the target being passed, pass both 'target.getX()' and 'target.getY()' (just for testing purposes, if nothing else).
erdelf erdelf

2012/6/29

#
the targets don't move so I use turnTowards in the bullets and I cant understand what your code would change
nccb nccb

2012/6/29

#
Looking at your code, it's not clear how the sniper knows which target to shoot, because you're not passing "target" to the shoot() method. What danpost is saying is that if at any point you have multiple targets in your scenario, the sniper may shoot at the wrong one. But to agree with SPower, it's really not enough code to figure out what the problem might be.
erdelf erdelf

2012/6/29

#
ok, here is the code. This in act in the world.
            Target target = new Target();
            int spawnX = Greenfoot.getRandomNumber(getWidth());
            int spawnY = Greenfoot.getRandomNumber(getHeight());
            addObject(target, spawnX, spawnY);
            sniper.turnTowards(target.getX(), target.getY());
            sniper.shoot();
this is shoot in sniper
    public void shoot()
    {
        getWorld().addObject(new Bullet(), getX(), getY());
    }
bullet act():
        if(!created)
        {
            AimbotWorld world = (AimbotWorld) getWorld();
            Sniper sniper = (Sniper) (world.getObjects(Sniper.class).get(0));
            setRotation(sniper.getRotation());
            created = true;
        }
        move(10);
        if(getOneIntersectingObject(Target.class) != null)
        {
            getWorld().removeObject(getOneIntersectingObject(Target.class));
            AimbotWorld.addSuccessed();
            getWorld().removeObject(this);
        } else if(atWorldEdge())
        {
            AimbotWorld.addFailed();
            getWorld().removeObject(this);
        }
the boolean atWorldEdge() is taken out of the importable animal class.
nccb nccb

2012/6/29

#
Ok. A few ideas of things that could be wrong: - If there is more than one Sniper in the world, the bullet could pick up its rotation from the wrong one - If the target is not very big, move(10) might be enough to move the bullet from one side of the target through to the other side, and thus not count as hitting it. - If two targets are spawned, one very near the sniper, and one far away, a bullet intended for the far away target will hit the nearer target and destroy it. Then, the bullet aimed at the near target won't hit the near one (already destroyed), but may well miss the far target (which it was never aiming at). That depends how often you're spawning targets -- do you spawn them all upfront, or one at a time?
erdelf erdelf

2012/6/29

#
there is only one. image is 60x71 every act run I spawn one. The last thing could be right. I will test a few things.
danpost danpost

2012/6/29

#
Try this in your world act:
Target target = null;
if (getObjects(Target.class).isEmpty()) 
{
    target = new Target();
    int spawnX = Greenfoot.getRandomNumber(getWidth());
    int spawnY = Greenfoot.getRandomNumber(getHeight());
    addObject(target, spawnX, spawnY);
}
if (getObjects(Bullet.class).isEmpty())
{
    target = (Target) getObjects(Target.class).get(0);
    sniper.turnTowards(target.getX(), target.getY());
    sniper.shoot();
}
erdelf erdelf

2012/6/29

#
thx men, it works. in over 10000 shots I never failed. ( you perfected aiming) Also there are never targets left. Thx again. Could you say me how? I don't understand everything of your code.
danpost danpost

2012/6/29

#
(1) Target target = null; just makes things a bit easier for coding; as you do not have to cast target in both 'if' blocks. (2) if (getObjects(Target.class).isEmpty()) Since we only want one target in the world at a time, we make sure none are in the world before adding one. The following code block adds one just as you we doing before. { target = new Target(); int spawnX = Greenfoot.getRandomNumber(getWidth()); int spawnY = Greenfoot.getRandomNumber(getHeight()); addObject(target, spawnX, spawnY); } (3) if (getObjects(Bullet.class).isEmpty()) Again, we do not want a stream of bullets going to the same target; so, we make sure no bullets are in the world before adding one. Again, basically the same code you were using. The only difference is we have to get a reference to the one target that is in the world at this point, hence the first line of code in the block. { target = (Target) getObjects(Target.class).get(0); sniper.turnTowards(target.getX(), target.getY()); sniper.shoot(); }
erdelf erdelf

2012/6/29

#
Would it be possible that more than one target is on the field?
There are more replies on the next page.
1
2