fig121-300x241.jpg  

stop();
  
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.events.NetStatusEvent;
import flash.net.GroupSpecifier;
  
var nc:NetConnection;
var group:NetGroup;
  
info_txt.text = "";
  
function connect():void {
    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
    nc.connect("rtmfp:");
}
  
function netStatus(evt:NetStatusEvent):void {
  
    switch(evt.info.code) {
  
        case "NetConnection.Connect.Success":
            log("連接成功");
            setupGroup();
            break;
        
        case "NetGroup.Connect.Success":
            log("NetGroup成功");
            break;
               case "NetGroup.Posting.Notify":
            log("NetGroup新傳檔");
            break;
    }
}
  
function setupGroup():void {
    var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/groupA");
    groupspec.postingEnabled = true;
    groupspec.ipMulticastMemberUpdatesEnabled = true;
    groupspec.addIPMulticastAddress("225.225.0.1:30000");
  
    group = new NetGroup(nc, groupspec.groupspecWithAuthorizations());
    group.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
  
    gotoAndStop(2);
}
  
function log(txt:String):void {
    info_txt.appendText(txt + "\n");
}
  
connect();

[フレーム2]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
stop();
  
import flash.geom.Point;
import flash.display.Sprite;
  
var highX:Number = 0;
var highY:Number = 0;
  
var lowX:Number = 0;
var lowY:Number = 0;
const FACTOR:Number = 0.5;
  
var moveX:Number = 0;
var moveY:Number = 0;
const MULTIPLIER:int = 96;
  
buta_mc.cacheAsBitmap = true;
  
var bg:Sprite = new Sprite();
addChildAt(bg, 0);
setBgColor(0x666666);
  
function setBgColor(col:int):void {
    bg.graphics.beginFill(col);
    bg.graphics.drawRect(0, 0, stage.stageWidth,stage.stageHeight);
    bg.graphics.endFill();
}
  
if (Accelerometer.isSupported)
{
    var fl_Accelerometer:Accelerometer = new Accelerometer();
    fl_Accelerometer.setRequestedUpdateInterval(24);
    fl_Accelerometer.addEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);
    addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
  
function fl_AccelerometerUpdateHandler(evt:AccelerometerEvent):void
{
    lowX = (evt.accelerationX *FACTOR)+(lowX*(1 - FACTOR));
    lowY = (evt.accelerationY *FACTOR)+(lowY*(1 - FACTOR));
  
    highX = evt.accelerationX - lowX;
    highY = evt.accelerationY- lowY;
  
    moveX = -1*(highX*MULTIPLIER);
    moveY = (highY*MULTIPLIER);
}
  
var maxNum:Number = 0;
  
function onEnterFrame(evt:Event):void {
    var oldPos:Point = new Point(buta_mc.x, buta_mc.y);
    var mMatrix:Matrix = buta_mc.transform.matrix;
    mMatrix.translate(moveX, moveY);
    buta_mc.transform.matrix = mMatrix;
    var newPos:Point = new Point(buta_mc.x, buta_mc.y);
    var dx:Number = oldPos.x - newPos.x;
    var dy:Number = oldPos.y - newPos.y;
    var distance:Number = Math.sqrt((dx*dx)+(dy*dy));
  
    if(distance > maxNum && distance > 100){
        maxNum = distance;
        var col:int = 0xFFFFFF*Math.random();
             var obj:Object = new Object();
        obj.color = col;
            obj.sender = group.convertPeerIDToGroupAddress(nc.nearID);
 
        group.post(obj);
        setBgColor(col);
    }
}
  
btn.addEventListener(MouseEvent.CLICK, onClick);
  
function onClick(evt:MouseEvent):void {
    maxNum = 0;
    isLocked = false;
    var obj:Object = new Object();
    var col:int = 0x666666;
    obj.color = col;
    obj.sender = group.convertPeerIDToGroupAddress(nc.nearID);
    group.post(obj);
    setBgColor(col);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.events.NetStatusEvent;
import flash.net.GroupSpecifier;
  
var nc:NetConnection;
var group:NetGroup;
  
function connect():void {
    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
    nc.connect("rtmfp:");
}
  
function netStatus(evt:NetStatusEvent):void {
    switch(evt.info.code) {
        case "NetConnection.Connect.Success":
            log("連接成功");
            setupGroup();
            break;
        case "NetGroup.Connect.Success":
            log("NetGroup成功");
            break;
        case "NetGroup.Posting.Notify":
            log("NetGroup傳檔");
            log("col : " + evt.info.message.color);
            setBgColor(int(evt.info.message.color));
            break;
    }
}
  
function setupGroup():void {
    var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/groupA");
    groupspec.postingEnabled = true;
    groupspec.ipMulticastMemberUpdatesEnabled = true;
    groupspec.addIPMulticastAddress("225.225.0.1:30000");
    group = new NetGroup(nc, groupspec.groupspecWithAuthorizations());
    group.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
}
  
connect();
  
 
var bg:Sprite = new Sprite();
addChildAt(bg, 0);
setBgColor(0x666666);
  
function setBgColor(col:int):void {
    bg.graphics.beginFill(col);
    bg.graphics.drawRect(0, 0, stage.stageWidth,stage.stageHeight);
    bg.graphics.endFill();
}
  
function log(txt:String):void {
    info_txt.appendText(txt + "\n");
}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()