【发布时间】:2013-11-23 23:11:49
【问题描述】:
我正在尝试弄清楚如何使黑球(在本例中标记为 bBall)朝着从白球 (wBall) 击中的方向移动,然后在击中一侧或角落时消失口袋,我该怎么做呢?
ball wBall, bBall;
int click;
String msg;
Boolean moving = false;
int difx, dify;
float cdistance;
int steps = 20;
void setup(){
click=0;
size(800,400);
background(16,77,27);
wBall = new ball(35,#ffffff);
bBall = new ball(35,#000000);
msg="";
}
void mouseClicked(){
if(!moving){
click++;
}
}
void draw(){
background(16,77,27);
String msg;
fill(0,0,0);
ellipse(15,15,30,30);
ellipse(785,15,30,30);
ellipse(15,385,30,30);
ellipse(785,385,30,30);
ellipse(410,15,30,30);
ellipse(410,385,30,30);
msg="the click count is "+click;
println("the click count is "+click);
//Moving Balls\\
fill(255,255,255);
noStroke();
bBall.xpos=(250);
bBall.ypos=height/2;
bBall.update();
if(click==0){
wBall.xpos=mouseX;
wBall.ypos=mouseY;
}else if(click==1){
difx = wBall.xpos-bBall.xpos;
dify = wBall.ypos-bBall.ypos;
}else if(click==2){
cdistance = dist(wBall.xpos,wBall.ypos,bBall.xpos,bBall.ypos);
if (cdistance>bBall.ballDiam/2){
moving = true;
wBall.xpos-=difx/steps;
wBall.ypos-=dify/steps;
}else{
moving = false;
wBall.visible=false;
click=3;
}
}
wBall.update();
}
class ball{
int xpos, ypos;
color myColor;
int ballDiam;
boolean visible = true;
ball(int tempdiam, color tempColor){
myColor=tempColor;
ballDiam=tempdiam;
}
void update(){
if(visible){
fill(myColor);
ellipse(xpos,ypos,ballDiam,ballDiam);
}
}
}
【问题讨论】:
标签: processing