Results 1 to 8 of 8
- 05-18-2010, 06:23 PM #1
Member
- Join Date
- May 2010
- Location
- Greece
- Posts
- 7
- Rep Power
- 0
Trouble creating object that includes array
Hello. I'm new in the world of Java and in this forum, so please excuse me if I'm posting in the wrong section or explaining my problem in a false manner and please dont refrain from noting it in your reply.
Well, here is the thing. I'm trying to code a small spaceship shooter game and this is my first time dealing with objects.
I have this class at my disposal
The Polygon class gets a series of point coordinates (inShape) that form a shape, the coordinates of this shape's current position (inPosition) and inRotation has nothing to do with my problem :PJava Code:public Polygon(Point[] inShape, Point inPosition, double inRotation)
The way Polygon works is, also, irrelevant (I think.)
I use the class Point as a means of storing coordinates. Here is the code:
Java Code:class Point { double x; double y; public Point(double inX, double inY) { x = inX; y = inY; } public Point clone() { return new Point(x, y); } }
And here is where the trouble is. I also have a class named Ship which represents the spaceship and is a subclass of Polygon.
In there i'm trying to define the shape of the spaceship by creating Point type objects, that I will then pass over to Polygon to process the shape and it's position in space. I cant create a Point[] Object successfully, i guess because am using the wrong syntax when trying to fill an array with coordinate pairs.Java Code:public class Ship extends Polygon {
These are some of my numerus failed experimental attempts to do it. I got frustrated so most of them are just random typing. They might not make any sense at all...
The basic error that occurs is : Syntax error on token ",", invalid AssignmentOperator. and it refers to the commas inside the coordinates' brackets. Here I should note that I work on Eclipse.Java Code:Point[] Alpha = {(0,0), (2,1), (4,0), (2,4)}; Point[] Alpha = new Point[] {(0,0), (2,1), (4,0), (2,4)}; Point[] Alpha = new Point[4] {(0,0), (2,1), (4,0), (2,4)}; Point[] Alpha = new Point[] [(0,0), (2,1), (4,0), (2,4)]; Point[] Alpha = new Point[] (0,0), (2,1), (4,0), (2,4); Point[] Alpha = new Point[] {(0.0), (2.1), (4.0), (2.4)};
Can anyone point out to me which is the ridiculus syntax mistake that tortures me? Thank you very much :D
- 05-18-2010, 06:28 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You were pretty close. Here's the correct syntax:
Java Code:Point[] array = {new Point(1,2), new Point(3,4)}Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-18-2010, 06:30 PM #3
Member
- Join Date
- May 2010
- Location
- Greece
- Posts
- 7
- Rep Power
- 0
oh gosh im gonna try this out right now! Thank you an awful lot :)
- 05-18-2010, 06:31 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
edit: too dang slowwww!Java Code:Point[] Alpha = {new Point(0,0), new Point(2,1), new Point(4,0), new Point(2,4)};
- 05-18-2010, 06:33 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
- 05-18-2010, 06:39 PM #6
try this
Java Code:public class PointExample { /** * @param args */ public static void main(String[] args) { Point[] Alpha = {new Point(0,0), new Point(2,1), new Point(4,0), new Point(2,4)}; int pointCount = 1; for (Point p : Alpha) { System.out.println("Point " + pointCount++ + " coordinates are: " + p.x + "," + p.y); } } }
and the output will be
have fun.Point 1 coordinates are: 0.0,0.0
Point 2 coordinates are: 2.0,1.0
Point 3 coordinates are: 4.0,0.0
Point 4 coordinates are: 2.0,4.0
- 05-18-2010, 06:59 PM #7
Member
- Join Date
- May 2010
- Location
- Greece
- Posts
- 7
- Rep Power
- 0
Okay, now how do I call super polygon from the Ship Class?
Java Code:public class Ship extends Polygon { Point[] Alpha = {new Point (0,0),new Point (2,1),new Point (4,0),new Point (2,4)}; public Ship(){ super (Alpha, new Point (3,5), 45); } }
- 05-18-2010, 07:33 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
The first thing a constructor does is either call another constructor in its class or call a superclass constructor. Either implicitly, i.e. the compiler inserts a "super()" call in your constructor or explicitly as you have done. The current object doesn't exist yet, i.e. it needs to be build up from the inside out: first the superclass constructor then comes your constructor afterwards.
Because the object of your class doesn't exist yet you can't refer to any member of it yet, as your Alpha array. Either make that Alpha array static or pass it in to your constructor as a parameter.
kind regards,
Jos
Similar Threads
-
Trouble with creating TreeSet
By MrKP in forum New To JavaReplies: 1Last Post: 12-27-2009, 02:22 AM -
trouble creating program using loops for multiplication table
By cuse17 in forum New To JavaReplies: 2Last Post: 02-23-2009, 02:18 AM -
Tomcat5: changing url (includes port and etc)
By happyknappy in forum Advanced JavaReplies: 1Last Post: 09-17-2008, 01:33 PM -
Need help with creating array of type object
By riz618 in forum New To JavaReplies: 3Last Post: 01-29-2008, 06:14 AM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks