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

2013/12/17

how to let my ship get a crate

16
17
18
19
20
21
danpost danpost

2014/1/10

#
I could not find any shark images in the images folder of the scenario. Maybe you mis-copied it
virginia001 virginia001

2014/1/10

#
solved it already
virginia001 virginia001

2014/1/11

#
I have question about making my shark do the following things: -I want it to remove a life when touching a ship. - I want to move in a sort of a rectangle area. - when touching touching the platforms i want it to look if they swim underneath it so they can't go on the platforms Nothing atm really works except that the shark is turning each time hes at the edge of the world I now have this code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shark here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shark extends Actor
{
    private Platform platform = null;
    
    public void act() 
    {
        move();
        Schip s = (Schip)getOneIntersectingObject(Schip.class);
        if (schip != null)
        {
            removeLive();
        }
    } 

    public void move()
    {
        move(5);
        Platform p = (Platform)getOneIntersectingObject(Platform.class);  
        
        if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
        {
            turn(180);
        }
        if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
        {
            turn(180);
        }
    }
    
    private Schip schip = null;  
    public void setSchip(Schip s)  
    {  
        schip = s;  
    }  
  
    public Schip getSchip()    
    {    
        return schip;    
    }
    
    public void removeLive()    
    {    
        int lives = getWorld().getObjects(Lives.class).size();    
        getWorld().removeObject((Actor)getWorld().getObjects(Lives.class).get(lives-1));  

        if (lives != 1)  
        {  
            //Only does the following, if it was the not the last last life  
            Greenfoot.playSound("lose life.wav");  
        }  
        else  
        {  
            //Only does the following, if it was the last life.  
            Greenfoot.playSound("game over.wav");  
            Greenfoot.stop();  
        }  
    }   
}
Super_Hippo Super_Hippo

2014/1/11

#
Line 17 should be 'if (s != null)'. You set 'schip' in line 38 to 'null', so it never removes a life. If you do it like this, you will lose all your lives at once if you do not remove the shark (or the ship and let it restart) after he touches the ship or set a boolean, so the shark will only remove one life if swims through the ship.
virginia001 virginia001

2014/1/11

#
So how do get the shark taking only one life if it hits the ship and the last two other things of my previous post
coldxms coldxms

2014/1/11

#
Virginia, you may also want to add a counter so that u as Super_hippo implies only get 1 life removed. an easy way to do so is: in the constructor: private int lifecounter = 100; in the act method put
    public void act() 
    {
        lifecounter--;
    }    
and another if statement:
    public void act()   
    {  
lifecounter--;
        move();  
        Schip s = (Schip)getOneIntersectingObject(Schip.class);  
        if (s != null && lifecounter <= 0)  
        {  
            removeLive();  
lifecounter = 100;
        }  
    }   
where lifecounter is the amount of "frames". on mid speed 100 is a nice time. Test it out! =)!
danpost danpost

2014/1/11

#
You can remove lines 38 through 47 as the shark does not have a ship object associated with it. You can also remove line 11 since the shark is not associated with any platform object either. And yes, as per Super_Hippo, line 17 should be 'if (s != null)'. As far as sharks 'swimming' above the other objects, add to your world constructor a 'setPaintOrder' call (refer to the documentation of the World class). It should be truck and crate before platform, include ship anywhere and add nothing else (everything else will be painted first and end up behind the objects of the classes listed). Again, Super_Hippo was correct in that if the shark is not to be removed when colliding with a ship object, then you will need an instance boolean field (call it 'atShip' to track the state of the shark being at a ship from one act to the next. This field is then used in conjunction with collision code to determine when to subtract a live and when to change the value of the field.
virginia001 virginia001

2014/1/11

#
The life function is good now but there are two little problems now, the shark collision isnt precise so i want that to be more precisely and when the crates are loading now the sound of loading isn't playing and there pops a terminal window that there is a problem with the soundeffects
danpost danpost

2014/1/11

#
Any time you ask about a problem that involves an error message, you should copy/paste the entire message you get into your post.
virginia001 virginia001

2014/1/11

#
this is just a little part of the error that pops up otherwise its two pages long... java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:400) at java.util.ArrayList.get(ArrayList.java:413) at Shark.removeLive(Shark.java:45) at Shark.act(Shark.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Exception in thread "Thread-6" java.lang.IllegalArgumentException: Format of sound file not supported: file:/C:/Users/Amir/Downloads/School/Project%20Game/Minigame%202%20vracht%20overslaan/sounds/ at greenfoot.sound.SoundExceptionHandler.handleUnsupportedAudioFileException(SoundExceptionHandler.java:56) at greenfoot.sound.SoundClip.open(SoundClip.java:118) at greenfoot.sound.SoundClip.processState(SoundClip.java:239) at greenfoot.sound.ClipProcessThread.run(ClipProcessThread.java:74) at java.lang.Thread.run(Thread.java:744) Caused by: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1153) at greenfoot.sound.ClipCache.getCachedClip(ClipCache.java:67) at greenfoot.sound.SoundClip.load(SoundClip.java:130) at greenfoot.sound.SoundClip.open(SoundClip.java:101) ... 3 more
danpost danpost

2014/1/11

#
It says that the audio file type is not supported. The documentation of the GreenfootSound class says "Most files of the following formats are supported: AIFF, AU, WAV, MP3 and MIDI."
virginia001 virginia001

2014/1/12

#
okay the sound problem is solved....When the new shark object collides with the ships is supposed to remove a life. ANd when it does it removes it but the problem is that the collision detection is real bad and it removes a life when theres a space of like 2 centimeters between eachother.. this is my code collision detection in the Shark class
Schip s = (Schip)getOneIntersectingObject(Schip.class);
        if (s != null && lifecounter <= 0)
        {
            removeLive();
            lifecounter = 100;
        }
danpost danpost

2014/1/12

#
If you draw a standard rectangle around the visual images, does the collision occur when these hypothetical rectangles touch or well before? If well before, there is probably a transparent border around one or both of your objects. It is when the images collide that detection is made, not when the visual images of the objects within the image boundaries collide.
virginia001 virginia001

2014/1/12

#
Sorry can you explain in a different way can't find out what you mean
Super_Hippo Super_Hippo

2014/1/12

#
Open the image with MS Paint (transparent => white) and see if the shark touches the borders of the image. Do not save the image.
There are more replies on the next page.
16
17
18
19
20
21