大概做了出来,只是有些摆放位置还不够准确。意思差不多了。自己再修改修改
第一步 写出秤杆(Chenggan.as)
package Tianping {
import flash.display.MovieClip;
import flash.display.Sprite;
public class Chenggan extends MovieClip {
public var leftpoint:Sprite ;
public var rightpoint:Sprite ;
private var allwigth:Number =0;
public function Chenggan() {
// constructor code
init();
}
private function init(){
leftpoint=new Sprite ;
rightpoint=new Sprite ;
leftpoint.x=-this.width /2
rightpoint.x=this.width /2;
this.addChild (leftpoint);
this.addChild (rightpoint);
}
}
}
第二步 托盘(Tuopan.as)
package Tianping {
import flash.display.MovieClip;
import com.greensock.TweenLite;
import com.greensock .easing .*;
import flash.geom.Point;
public class Tuopan extends MovieClip {
public var target:ChengMc ;
private var weight:Number=0 ;
public function Tuopan() {
// constructor code
}
public function addThings(tin:ThingMc ){
if(tin.states){
weight=weight-tin.weigth ;
TweenLite.to (tin,0.5,{x:0,y:0});
}else{
TweenLite.to (tin,0.5,{x:this.x+170,y:this.y+190});
weight=weight+tin.weigth ;
}
target.ChengDongxi ();
}
public function get weights():Number {
return weight;
}
}
}
第三步 组装一个简易的称(ChengMc.as)
package Tianping {
import flash.display.Sprite;
import com.greensock .TweenLite;
import com.greensock .easing .*;
public class ChengMc extends Sprite {
private var _gan:Chenggan ;
public var _lefttuo:Tuopan ;
public var _righttuo:Tuopan ;
public function ChengMc() {
// constructor code
Initui()
}
private function Initui(){
_gan=new Chenggan
_lefttuo=new Tuopan;
_righttuo=new Tuopan;
_lefttuo.x=_gan.leftpoint .x;
_righttuo.x=_gan.rightpoint.x;
_righttuo.target =_lefttuo.target =this;
_gan.addChild (_lefttuo);
_gan.addChild (_righttuo);
this.addChild(_gan);
}
public function ChengDongxi(){
var leftweight:Number =_lefttuo.weights;
var rightweigth:Number =_righttuo.weights;
var needrot:Number =Math.abs (leftweight-rightweigth);
if(leftweight>rightweigth){
TweenLite.to (_gan,0.5,{rotation:-needrot});
}else {
TweenLite.to (_gan,0.5,{rotation:needrot});
}
}
}
}
第四部 写出一个物品类(ThingMc.as)
package Tianping {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class ThingMc extends Sprite {
private var objName:String ="土豆";
private var weight:Number =1;
private var haveadd:Boolean=true;
public function ThingMc(n:String ,w:Number ) {
objName=n;
weight=w;
// constructor code
initui();
this.addEventListener (MouseEvent.CLICK,clicknow)
}
private function clicknow(e:MouseEvent ){
}
public function changestaget(){
if(haveadd){
haveadd=false;
}else{
haveadd=true
}
}
private function initui(){
var nametext:TextField =new TextField
nametext.htmlText =objName;
nametext.mouseEnabled =false;
this.addChild (nametext);
}
public function get weigth():Number {
return weight;
}
public function get states():Boolean {
return haveadd;
}
}
}
第五步 制作一个简单的 应用环境 ,即是 新建一个FLA文档:注明 我这个是AS3.0写的,用的是FLASH CS6 在该文档第一针上写入一下代码
import Tianping.ChengMc;
import Tianping.ThingMc;
import flash.events.MouseEvent;
var cheng:ChengMc=new ChengMc
cheng.x=200
cheng.y=200
this.addChild (cheng);
var fanqie:ThingMc =new ThingMc ("番茄",6.5)/////////番茄是名字,6.5是重量,这个由你自己决定了
var qiezi:ThingMc =new ThingMc ("茄子",10.5)
qiezi.x=150;
this.addChild (fanqie)
this.addChild (qiezi)
fanqie.addEventListener (MouseEvent.CLICK,toclick)
qiezi.addEventListener (MouseEvent.CLICK,toclickqiezi)
function toclick(e:MouseEvent ){
fanqie.changestaget ();
cheng._lefttuo.addThings (fanqie)
}
function toclickqiezi(e:MouseEvent ){
qiezi.changestaget ();
cheng._righttuo.addThings (qiezi)
}
///运行截图
附件下载
谢谢你的详细回答,但是我玩了一下,是按物品之后它自动放到天平上,还固定位置了,我需要的是随机放置哦
ni可以把那个位置 写成随机就是了撒!托盘部分的这一句
TweenLite.to (tin,0.5,{x:this.x+170,y:this.y+190});
这样写位置就是固定的,你把这个位置用random() 设置不就可以了
很多地方你要是做好 肯定还需要你自己修改撒,我只是把大纲 给你拟定好了,你自己按照我这个往里面填代码才能做完美。包括UI部分
2024-09-19 广告
2014-06-03
= =PS怎么写代码啊,要互动的