默然说话

一个异想天开的人做着异想天开的梦

牟勇ID:mouyong
64387次访问,排名1577好友4人,关注者6
我快乐,我存在
mouyong的文章
原创 108 篇
翻译 4 篇
转载 30 篇
评论 12 篇
默然的公告
如果要联系我,希望能说明来意,谢谢.

点击这里给我发消息

Google

最近评论
peigen:又~~~~为什么是又呢???
dcopperfield:顶下
gaoyunpeng:无意中进入到这个博客,很快就被里面的内容所吸引,感觉很有意思,不知道为什么会有这样的感觉,或许只是一种直觉上的吸引吧,一直在看博客里的文章,觉得很不错,天天等更新,哈哈,终于看到新的文章啦~
我会一直关注的~
mouyong:谢谢你的鼓励,我会更加努力。
了祝愿你实现自己的理想,达成自己的目标
wsspy007:牟老师,我发现你是我见过最好的老师了,为学生考虑最多,一切为学生着想,不知道牟老师还记得我么,张伟(无名小辈肯定你是忘记了),第一期跟杨大宇他们在一个班的,补考两次都没有及格,我现在不在昆明了,在胜利油田,这里一切都很好,一个月的薪金能顶得上昆明3-5个月的薪水,但是我不喜欢这份工作,钱固然多,但是从学校出来步入社会以后才发现,我还是应该走软件开发这条路,现在每天我只睡4-6个小时,工作12……
文章分类
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 Java2游戏编程读书笔记中的源码错误收藏

    新一篇: Java2游戏编程读书笔记(11-2) | 旧一篇: Java2游戏编程读书笔记(11-1)

    Java2游戏编程读书笔记(10-1)中的Actor2D类的默认构造方法中写漏几句代码(现已更新,错误已被更正),在此向广大读者致歉。给你们带来了不便请原谅。

    你可以重新复制(10-1)中的Actor2D类(现已修改),或者将下面的代码复制并替换Actor2D类的代码(漏写代码用红色标出):

    import java.awt.*;
    import java.awt.geom.*;
    import java.util.*;

    //这个类封装了移动和绘制一个2-D游戏对象所需要的一般信息
    public abstract class Actor2D extends Object
            implements Moveable{
     //一个actor具有的一般状态
     public final int STATE_ALIVE=1;
     public final int STATE_DYING=2;
     public final int STATE_DEAD=4;
     
     //这个actor的当前状态
     protected int state;
     
     //所属的角色组
     protected ActorGroup2D group;
     
     //actor位置,速度和旋转,又及缓存的变换
     protected Vector2D pos;
     protected Vector2D vel;
     protected double rotation;
     protected AffineTransform xform;
     
     protected final double TWO_PI=2*Math.PI;
     
     //冲突检测用的边界矩形
     protected Rectangle2D bounds;
     
     //和这个actor有冲突的actor列表
     protected LinkedList collisionList;
     
     //宽和高
     protected int frameWidth;
     protected int frameHeight;
     
     //指向当前动画条的引用
     protected AnimationStrip currAnimation;
     
     //播放下一帧之前需要等待的帧数和一个帧计数器
     protected int animWait;
     protected int animCount;
     
     //创建一个属于给定ActorGroup的Actor2D对象
     public Actor2D(ActorGroup2D grp){
      group=grp;
      bounds=new Rectangle2D.Double();
      collisionList=new LinkedList();
      
      state=0;
      
      pos=new Vector2D.Double();
      vel=new Vector2D.Double();
      rotation=0;
      xform=new AffineTransform();
      
      currAnimation=null;
      animWait=0;
      animCount=0;
      
      frameWidth=0;
      frameHeight=0;
     }
     
     //每隔animWait帧,移动一下actor
     public void animate(){
      if(currAnimation!=null){
       if(++animCount>=animWait){
        currAnimation.getNextFrame();
        animCount=0;
       }
      }
     }
     
     //使用它自的变换来绘制actor
     public void paint(Graphics2D g2d){
      if(currAnimation!=null){
       g2d.drawImage(currAnimation.getCurrFrame(),
           xform,AnimationStrip.observer);
      }
     }
     
     //在(x,y)坐标处绘制actor
     public void paint(Graphics2D g2d,double x,double y){
      if(currAnimation!=null){
       g2d.drawImage(currAnimation.getCurrFrame(),
           AffineTransform.getTranslateInstance(x,y),
           AnimationStrip.observer);
      }
     }
     
     //简单边界盒,判断actor是否和传入的actor冲突
     public boolean intersects(Actor2D other){
      return bounds.intersects(other.getBounds());
     }
     
     //根据当前的x和y位置更新边界盒
     public void updateBounds(){
      //确保知道actor的正确的宽度和高度
      if(frameWidth<=0&&currAnimation!=null){
       frameWidth=currAnimation.getFrameWidth();
      }
      
      if(frameHeight<=0&&currAnimation!=null){
       frameHeight=currAnimation.getFrameHeight();
      }
      
      bounds.setRect(pos.getX(),pos.getY(),
          frameWidth,frameHeight);
     }
     
     //确保actor的边界没有超出actor组所限定的边界
     public void checkBounds(){
      if(group==null){
       return;
      }
      
      if(bounds.getX()<group.MIN_X_POS){
       pos.setX(group.MIN_X_POS);
      }else if(bounds.getX()+frameWidth>group.MAX_X_POS){
       pos.setX(group.MAX_X_POS-frameWidth);
      }
      
      if(bounds.getY()<group.MIN_Y_POS){
       pos.setY(group.MIN_Y_POS);
      }else if(bounds.getY()+frameHeight>group.MAX_Y_POS){
       pos.setY(group.MAX_Y_POS-frameHeight);
      }
     }
     
     //返回一个描述这个actor的字符串
     public String toString(){
      return super.toString();
     }
     
     //把传入的值和当前的属性值进行位或操作
     public final void setState(int attr){
      state|=attr;
     }
     
     //使用位与或者非重新设置属性
     public final void resetState(int attr){
      state&=~attr;
     }
     
     public final int getState(){
      return state;
     }
     
     public final void clearState(){
      state=0;
     }
     
     //判断所传入的状态属性是否被actor的状态属性所包含
     public final boolean hasState(int attr){
      return ((state & attr)!=0);
     }
     
     //actor的速度,位置和旋转的访问方法
     public final void setX(double px){
      pos.setX(px);
     }
     
     public final void setY(double py){
      pos.setY(py);
     }
     
     public final double getX(){
      return pos.getX();
     }
     
     public final double getY(){
      return pos.getY();
     }
     
     public final void setPos(int x,int y){
      pos.setX(x);
      pos.setY(y);
     }
     
     public final void setPos(double x,double y){
      pos.setX(x);
      pos.setY(y);
     }
     
     public final void setPos(Vector2D v){
      pos.setX(v.getX());
      pos.setY(v.getY());
     }
     
     public final Vector2D getPos(){
      return pos;
     }
     
     public final void setRot(double theta){
      rotation=theta;
     }
     
     public final double getRot(){
      return rotation;
     }
     
     public final void rotate(double theta){
      rotation+=theta;
      
      while(rotation>TWO_PI){
       rotation-=TWO_PI;
      }
      
      while(rotation<-TWO_PI){
       rotation+=TWO_PI;
      }
     }
     
     public final void setVel(int x,int y){
      vel.setX(x);
      vel.setY(y);
     }
     
     public final void setVel(Vector2D v){
      vel.setX(v.getX());
      vel.setY(v.getY());
     }
     
     public final Vector2D getVel(){
      return vel;
     }
     
     public final void moveBy(double x,double y){
      pos.translate(x,y);
     }
     
     public final void moveBy(int x,int y){
      pos.translate(x,y);
     }
     
     public final void moveBy(Vector2D v){
      pos.translate(v);
     }
     
     public final void accelerate(double ax,double ay){
      vel.setX(vel.getX()+ax);
      vel.setY(vel.getY()+ay);
     }
     
     public int getWidth(){
      return frameWidth;
     }
     
     public int getHeight(){
      return frameHeight;
     }
     
     //从Moveable接口所继承的方法
     public Rectangle2D getBounds(){
      return bounds;
     }
     
     //判断一个Moveable对象是否和这个对象冲突
     public boolean collidesWith(Moveable other){
      return (bounds.contains(other.getBounds())||
        bounds.intersects(other.getBounds()));
     }
     
     //在冲突列表中添加一个冲突对象
     public void addCollision(Moveable other){
      if(collisionList==null){
       collisionList=new LinkedList();
       collisionList.add(other);
       return;
      }
      
      if(!collisionList.contains(other)){
       collisionList.add(other);
      }
     }
     
     //处理与冲突列表中对象之间冲突的stub方法
     //这个方法留空,但并未声明为抽象
     public void processCollisions(){
     }
     
     //更新对象的位置和边界盒,移动它,然后更新变换
     public void update(){
      pos.translate(vel);
      updateBounds();
      checkBounds();
      
      animate();
      
      //子类如果要覆盖这个方法,必须要求变换是以原点为中心的
      //而不是位置以原点为中心
      if(rotation !=0){
       xform.setToIdentity();
       xform.translate(pos.getX()+frameWidth/2,
           pos.getY()+frameHeight/2);
       xform.rotate(rotation);
       xform.translate(-frameWidth/2,-frameHeight/2);
      }else{
       xform.setToTranslation(pos.getX(),pos.getY());
      }
     }
    }//Actor2D 

    发表于 @ 2007年05月01日 13:55:00|评论(loading...)|编辑

    新一篇: Java2游戏编程读书笔记(11-2) | 旧一篇: Java2游戏编程读书笔记(11-1)

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 默然