Copy and Pasted code for Trajectory and got one parsing error...
this is where I copy and pasted the code from and when I paste it into unity I get a parsing error around the world extends in the first line
public static class TrajectoryActor extends Actor {
private Controller controller;
private ProjectileEquation projectileEquation;
private Sprite trajectorySprite;
public int trajectoryPointCount = 30;
public float timeSeparation = 1f;
public TrajectoryActor(Controller controller, float gravity, Sprite trajectorySprite) {
this.controller = controller;
this.trajectorySprite = trajectorySprite;
this.projectileEquation = new ProjectileEquation();
this.projectileEquation.gravity = gravity;
}
@Override
public void act(float delta) {
super.act(delta);
projectileEquation.startVelocity.set(controller.power, 0f);
projectileEquation.startVelocity.rotate(controller.angle);
}
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
float t = 0f;
float width = this.width;
float height = this.height;
float timeSeparation = this.timeSeparation;
for (int i = 0; i < trajectoryPointCount; i++) {
float x = this.x + projectileEquation.getX(t);
float y = this.y + projectileEquation.getY(t);
batch.setColor(this.color);
batch.draw(trajectorySprite, x, y, width, height);
t += timeSeparation;
}
}
@Override
public Actor hit(float x, float y) {
return null;
}
}
Answer by Suddoha · Dec 06, 2015 at 12:41 AM
The code you've posted is Java, so neither completely compatible with C# without a few changes nor the same as UnityScript. It's also neither obvious if you wanted to use this as C# or UnityScript (kind of JavaScript which is NOT Java).
As for the error, C# uses a colon for inheritance instead of 'extends'. But this won't help you much. You also need the base class 'Actor' which is written in Java too (or re-implement that in your TrajectoryActor class) and the ones acting as attributes here, 'Controller, 'ProjectileEquation'. 'Sprite' exists in Unity, but it won't be the one you expect . If you have a closer look at the code, there's also the 'SpriteBatch' class etc. Lot's more to note, for example the annotations (the 'at'Override etc.) aren't supported in C#, that's pretty much Java specific. In order to override methods in C#, you'd need to have a method marked 'virtual' in the base class and override it with the keyword 'override' in the derived class.
I won't continue to list all the differences and difficulties for a proper conversion here, all I can really do is to suggest you to head on to the Unity Tutorial series and when you feel comfortable enough with one of the languages, start implementing this by yourself or find a proper C# source that you can copy most of the things from.
Your answer
Follow this Question
Related Questions
How to fix this parsing error? 2 Answers
Parsing error 1 Answer
Error CS0118: Armor.DisplayName is a property but a type was expected 0 Answers
Editor is Broken 1 Answer