Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java and jbuilder help with asteroids code

Options
  • 15-02-2004 9:37pm
    #1
    Closed Accounts Posts: 7,134 ✭✭✭


    hi folks

    i am having problems compiling this code on jbuilder x.

    i get a radius has private access in sprite at line 845 error??

    the error is on the line with asterix.

    can anyone help me out?

    EM :)





    /************************************************************************************************
    Asteroids.java
    Original code by Mike Hall, www.brainjar.com, 1998.
    Altered by Rudy Rucker, rucker@mathcs.sjsu.edu, 1999.

    Code last revised December 23, 1999, by Rudy Rucker.


    *********************************************/

    import java.awt.*;
    import java.net.*;
    import java.util.*;
    import java.applet.*;
    //BEGIN COMMENT OUT FOR JDK 1.0 ============

    //Need this to build as application, need WindowAdapter in AsteroidsFrame.
    import java.awt.event.*;

    //End COMMENT OUT FOR JDK 1.0 ============



    }
    } //End disk case.
    else //!ownerApp.game.diskflag means rectangular world
    {
    if (bounce)
    { //bounce off walls
    if (position.x < radius-Units.WIDTH_WORLD / 2)
    { velocity.x *= -1;
    position.x = radius-Units.WIDTH_WORLD / 2;
    outcode = true;
    }
    if (position.x > -radius+Units.WIDTH_WORLD / 2)
    { velocity.x *= -1;
    position.x = -radius+Units.WIDTH_WORLD / 2;
    outcode = true;
    }
    if (position.y < radius-Units.HEIGHT_WORLD / 2)
    { velocity.y *= -1;
    position.y = radius-Units.HEIGHT_WORLD / 2;
    outcode = true;
    }
    if (position.y > -radius+Units.HEIGHT_WORLD / 2)
    { velocity.y *= -1;
    position.y = -radius+Units.HEIGHT_WORLD / 2;
    outcode = true;
    }
    }
    else
    { //wrap to other side
    if (position.x < -Units.WIDTH_WORLD / 2)
    {position.x += Units.WIDTH_WORLD;
    outcode = true;}
    if (position.x > Units.WIDTH_WORLD / 2)
    {position.x -= Units.WIDTH_WORLD;
    outcode = true;}
    if (position.y < -Units.HEIGHT_WORLD / 2)
    {position.y += Units.HEIGHT_WORLD;
    outcode = true;}
    if (position.y > Units.HEIGHT_WORLD / 2)
    {position.y -= Units.HEIGHT_WORLD;
    outcode = true;}
    }
    } //End rectangular world
    return outcode;
    }



    /** Applies the rotation and the position translation to the shape to get the
    shapetransform polygon. The shape is rotated angle degrees around the origin
    and then translated by the amount pixel*position + (width/2, height/2).*/
    public void render()
    {
    int i;
    RealPixelConverter rtopc = ownerApp.realpixelconverter;
    shapetransform = new Polygon();
    rtopc.transform(position, positiontransform);
    double scale = rtopc.scale();
    double scalecos = scale*Math.cos(angle);
    double scalesin = scale*Math.sin(angle);
    for (i = 0; i < shape.npoints; i++)
    /* I write out the transform operation by hand here to make it faster.
    I am doing three things to each vertex of the polygon (1) rotate it
    around the origin, (2) carry out the RealPixelConverter transform, which
    means multiplying each term by scale or -scale, (3) add the point to
    the positiontransform location.
    I need to do Math.max, because the IE 4 browser treats negative pixel
    coordinates a large positives, making a poly with a vertex offscreen to top
    or left be drawn as if that vertex were at positive infinity, making a
    horizontal or vertical line. IE 5 and Netscape don't do this. Java: write
    once debug everywhere. */
    shapetransform.addPoint(
    Math.max(0,(int)Math.round(
    scalecos*shape.xpoints -
    scalesin*shape.ypoints +
    positiontransform.x)),
    Math.max(0, (int)Math.round(
    -(scalecos*shape.ypoints + //Flip sign because pixel y runs down.
    scalesin*shape.xpoints) +
    positiontransform.y)));
    }

    /** Just look at the distances between the positions of the two and compare to
    the sum of the radii. This is a faster method than isTouching, though
    less accurate. We use this for asteroid-asteroid collisions only. It is
    good for this purpose becuase it makes the collision back-off correction look
    smooth. */
    public boolean isColliding(Sprite s)
    {
    return (position.distanceTo(s.position) < radius + s.radius);
    }

    /** This is more accurate than isColliding, but a bit slower.
    We use it for the ship-asteroid collisions, for ship-bullet, and for
    bullet-missile collisions. You have to use inside for JDK 1.0, but don't
    use it for JDK 1.1 as it doesn't work there. */
    public boolean isTouching(Sprite s)
    {
    int i;
    if (JDK.version == 0)
    {
    for (i = 0; i < s.shapetransform.npoints; i++)
    if (shapetransform.inside( //contains method for JDK 1.1
    s.shapetransform.xpoints,s.shapetransform.ypoints))
    return true;
    for (i = 0; i < shapetransform.npoints; i++)
    if (s.shapetransform.inside( //contains method for JDK 1.1
    shapetransform.xpoints, shapetransform.ypoints))
    return true;
    }
    else
    {
    //BEGIN COMMENT OUT FOR JDK 1.0=========

    for (i = 0; i < s.shapetransform.npoints; i++)
    if (shapetransform.contains(
    s.shapetransform.xpoints,s.shapetransform.ypoints))
    return true;
    for (i = 0; i < shapetransform.npoints; i++)
    if (s.shapetransform.contains(
    shapetransform.xpoints, shapetransform.ypoints))
    return true;

    //END COMMENT OUT FOR JDK 1.0=======
    }
    return false;
    }

    /** The abstract reset method is called by the child sprite constructors, and
    may also be called when resetting a sprite's state.*/
    public abstract void reset();

    /** The update method can be overloaded by the child sprite. */
    public abstract void update();

    /** The base method bails out if the the Asteroids soundsloaded isn't
    true, and it won't turn on a sound if Asteroids soundflag is off. */
    public boolean loopsound(boolean onoff)
    {
    if (ownerApp == null)
    return false;
    if (!(ownerApp.triedgettingsounds && ownerApp.soundsloaded))
    return false;
    if (onoff && !ownerApp.soundflag)
    return false;
    soundplaying = onoff; //The value you'd like to do.
    return true;
    }

    /** The stop method sets active to false and turns off the sound. */
    public void stop()
    {
    active = false;
    ageDone = age;
    loopsound(false);
    }
    /** Create sprites for explosion animation. Each individual line segment
    of the given shapetransform is used to create a new shapetransform that will
    move outward from the shapetransform's original position with a random
    rotation. We deliberately don't call setRadius for the explosions because
    we want their centers not to be in the middle of their segments. Instead
    we set their radii by hand. */
    public void explode()
    {
    int skip, i, j;
    SpriteExplosion explosion;
    skip = 1;
    if (shape.npoints >= 12)
    skip = 2;
    for (i = 0; i < shape.npoints; i += skip)
    {
    explosion = ownerApp.game.nextExplosion();
    explosion.active = true;
    explosion.shape = new Polygon();
    explosion.shape.addPoint(shape.xpoints, shape.ypoints);
    j = i + 1;
    if (j >= shape.npoints)
    j -= shape.npoints;
    explosion.shape.addPoint(shape.xpoints[j], shape.ypoints[j]);
    explosion.angle = angle;
    ******* >>>> explosion.radius = radius; *************************
    explosion.angularAcceleration = ownerApp.randomizer.nextDouble(
    -Units.MAX_TURN_SPEED_EXPLOSION, Units.MAX_TURN_SPEED_EXPLOSION);
    explosion.position.copy(position);
    explosion.velocity.set(-shape.xpoints, -shape.ypoints);
    explosion.velocity.multiply(Units.SPEED_MULTIPLIER_EXPLOSION);
    explosion.ageDone = explosion.age + Units.LIFETIME_EXPLOSION;
    explosion.render();
    }
    }


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Originally posted by x in the city
    hi folks

    i am having problems compiling this code on jbuilder x.

    i get a radius has private access in sprite at line 845 error??

    the error is on the line with asterix.

    can anyone help me out?

    EM :)
    I'm guessing that in the SpriteExplosion Object (or one of its superclasses, the variable radius is protected/private. So it'll have setRadius() and getRadius() methods instead. You say you explicitly want to change it, but the setRadius() method should be no different. If the SpriteExplosion class is one of your own, then it's simply a matter of changing the variable declaration for radius, to public instead of private, within the SpriteExplostion class.


Advertisement