NCF参数化建筑论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 12257|回复: 9
打印 上一主题 下一主题

[在线求助] 新手Processing代码求教

[复制链接]
1m
发表于 2014-8-10 16:14:29 | 显示全部楼层
你混淆了class 和Object的概念。
你的Mover是一个class,你需要在这个class下定义一个变量。

修改过的代码:
class ClassNameMover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float mass;

  ClassNameMover() {
    mass=1;
    location=new PVector( 30, 30);
    velocity=new PVector (0, 0);
    acceleration= new PVector(0, 0);
  }

  void applyForce(PVector force) {
    PVector f = PVector.div(force, mass);
    acceleration.add(f);
  }
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    acceleration.mult(0);
  }
  void display() {
    stroke(0);
    fill(175);
    ellipse(location.x, location.y, mass*16, mass*16);
  }
  void checkedge() {
    if (location.x>width) {
      location.x=width;
      velocity.x*=-1;
    } else if (location.x < 0) {
      velocity.x *= -1;
      location.x = 0;
    }
    if (location.y > height) {
      velocity.y *= -1;
      location.y = height;
    }
  }
}


PVector wind= new PVector (0.01, 0);
PVector gravity= new PVector (0, 0.1);
ClassNameMover Mover;


void setup() {
  size(200, 500);
  background(255);
  Mover=new ClassNameMover();
}

void draw() {

  Mover.applyForce(wind);
  Mover.applyForce(gravity);
  Mover.update();
  Mover.display();
  Mover.checkedge();
}


我这里图省事把你的class名称改成了 ClassNameMover,定义的object变量沿用了你之前的Mover;

小黑屋|手机版|NCF参数化建筑论坛 ( 浙ICP备2020044100号-2 )    辽公网安备21021102000973号

GMT+8, 2024-6-10 23:25 , Processed in 0.056373 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表