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

Report as inappropriate.

Malmotri
Malmotri presents ...

2012/11/9

Lab 5 – Shot from a cannon

No description.

3611 views / 7 in the last 7 days

Tags: with-source

open in greenfoot
Your browser does not support the canvas tag.
jabirfatah91jabirfatah91

2012/11/9

OOps!! Correction: And dude, isn't it possible to add a message which can tell me if the target was HIT or not?
MalmotriMalmotri

2012/11/10

The scrolling is not used for target. Is gonna be used for wind. The game is not complete yet. A target should be added soon.
jabirfatah91jabirfatah91

2012/11/10

Okay!! Thanks! Can you please explain these codes in your ball class for me ? (is given below): import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ball here. * * @author (your name) * @version (a version number or a date) */ public class Ball extends Actor { /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ //double power = 30; //private double angle = 30; private double h0 = 1; private double h = h0; private double tStart; private double xStart; private double yStart; private double g = 30; private double SCALE = 7; double v0y; double v0x; private boolean first = true; public Ball(int angle, int power) { v0y = power * Math.sin(Math.toRadians(angle)); v0x = power * Math.cos(Math.toRadians(angle)); } public void act() { if(first) { first = !first; tStart = System.currentTimeMillis(); xStart = getX(); yStart = getY(); } else { double t = (System.currentTimeMillis() -tStart) / 1000; h = h0 +(v0y*t) - ( (g*t*t) / 2); setLocation((int)(xStart + SCALE * (v0x*t)),(int)(yStart - SCALE *h)); } removeBall(); } public void removeBall() { World background = getWorld(); if (getY() > background.getHeight()) { background.removeObject(this); } } } Question 1: Why do we need "private double" here? 2: "private boolean first = true"- what is this for? 3: first = !first; tStart = System.currentTimeMillis();- what does those codes mean? and what does "TimeMillis" do here?
danpostdanpost

2012/11/10

@jabirfatah91, (1) 'private' is not neccessary, but will restrict access from other classes; 'double' is used for more precise movement. (2) with 'first' initially set to 'true', the code block that is executed on the condition 'if (first)' will execute. (3a) Since 'first' is changed within the block to 'false' (by 'first = !first;'), the block is destined to only run once (the first time the act method is executed). (3b) 'System.currentTimeMillis()' is a system command to return the 'long' value of the current system time. Each unit of time returned is equivalent to 1/1000th of a second.
A new version of this scenario was uploaded on Sun Nov 11 18:06:39 UTC 2012 Keypress Wind = w - e Power = up - down Angle = left - right Fire = space Everything is not done yet. A target, and a display showing current power and score will be implemented soon.
A new version of this scenario was uploaded on Sun Nov 11 18:39:50 UTC 2012
A new version of this scenario was uploaded on Sun Nov 11 21:28:55 UTC 2012 Target added
A new version of this scenario was uploaded on Mon Nov 12 00:17:19 UTC 2012
A new version of this scenario was uploaded on Mon Nov 12 12:28:12 UTC 2012
A new version of this scenario was uploaded on Mon Nov 12 14:41:42 UTC 2012

See all comments

Want to leave a comment? You must first log in.

Who likes this?

No votes yet.