Thursday, July 29th 2010, 1:39pm UTC+2

You are not logged in.

  • Login
  • Register

public

Moderator

Posts: 546

Location: Dessau

1

Tuesday, February 5th 2008, 5:29am

[STUFF] Vector, Iterator, Broadcaster

hier ma n kleines bsp ... für einen Vector und einen Iterator und ein minimale anwendungs bsp für einen Broadcaster

Interface IList

ActionScript-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package de.djpublic.utils{
    
    public interface IList{
        
        function size():int;
        function addElement(object:Object):void;
        function removeElement(object:Object):void;
        function add(object:Object):Boolean;
        function remove(object:Object):Boolean;
        function getElement(index:int):Object;
        function getIndex(object:Object):int;
        function toArray():Array;
        function removeAt(index:int):void;
    }
    
}

Klasse Vector

ActionScript-Quelltext

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package de.djpublic.utils{
    
    public class Vector implements IList{
        
        
        private var _arr:Array;
        private var _size:int;
        
        /**
        *
        */
        public function size():int{
            this._size this._arr.length;
            return this._size;
        }
        
        /**
        *
        */
        public function Vector(){
            this._arr = new Array();
            this._size 0;
        }
        
        /**
        *
        */
        public function addElement(object:Object):void{
            this._arr.push(object);
        }
        
        /**
        *
        */
        public function removeElement(object:Object):void{
            var s:int this.size();
            
            for(var i:int 0s++){
                if(this._arr[i] == object){
                    this._arr.splice(i1);
                }
            }
        }
        
        /**
        *
        */
        public function add(object:Object):Boolean{
            var s:int this.size();
            
            for(var i:int 0s++){
                
                if(this._arr[i] == object){
                    
                    return false;
                    break;
                }
                
                
                
            }
            this._arr.push(object);
            return true;
        }
        
        /**
        *
        */
        public function remove(object:Object):Boolean{
            var s:int this.size();
            
            for(var i:int 0s++){
                if(this._arr[i] == object){
                    this._arr.splice(i1);
                    return true;
                    break;
                }
            }
            return false;
        }
        /**
        *
        */
        public function removeAt(index:int):void{
            this._arr.splice(index1);
        }
        
        /**
        *
        */
        public function getElement(index:int):Object{
            return this._arr[index];
        }
        
        
        /**
        *
        */
        public function getIndex(object:Object):int{
            var s:int this.size();
            var index:int 0;
            
            for(var i:int 0s++){
                index i;
                if(this._arr[i] == object){
                    break;
                }
            }
            return index;
        }
        
        /**
        *
        */
        public function toArray():Array{
            return this._arr;
        }
        
         
    }
    
    
}

Klasse Iterator

ActionScript-Quelltext

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
package de.djpublic.utils{
    
    public class ListIterator{
        
        private var _cursor:int;
        private var _size:int;
        private var _list:IList;
        
        public function ListIterator(list:IList){
            this._cursor 0;
            this._list = list;
            this._size this._list.size();
        }
        
        public function next():Object{
            return this._list.getElement(this._cursor ++);
        }
        
        public function hasNext():Boolean{
            return this._cursor this._size;
        }
        
        public function get cursor():int{
            return this._cursor;
        }
        
    }
    
}


Klasse Broadcaster

ActionScript-Quelltext

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
package de.djpublic.utils{
    
    import de.djpublic.utils.*;
    
    public class Broadcaster{
        
        private var _listeners:Vector;

        
        
        public function Broadcaster(){
            this._listeners = new Vector();
        }
        
        public function addListener(listener:Object):void{
            this._listeners.add(listener);
            
        }
        
        public function removeListener(listener:Object):void{
            this._listeners.remove(listener);
        }
        
        public function fire(evt:String, ...rest):void{
            
            var it:ListIterator = new ListIterator(this._listeners);

            while(it.hasNext()){
                var handler:* = it.next()[evt];
                if(handler != undefinedhandler.apply(it.next(), rest);
            }
        }
        
    }
    
}


Klasse TestBroadcaster

ActionScript-Quelltext

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
package de.djpublic.utils{
    
    import de.djpublic.utils.*;
    
    public class TestBroadcaster extends Broadcaster{
        
        private var _count:int;
        
        public function get count():int{
            return this._count;
        }
        public function TestBroadcaster(){
            this._count 0;
            
        }
        
        public function start(){
            
            fire("onStart");
            for(var i:int 010++){
                this._count i;
                fire("onChange"this.count);
                
            }
            this.fire("onFinish");
            
            
        }
    }
    
}


test fla

ActionScript-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import de.djpublic.utils.*;

var test:TestBroadcaster = new TestBroadcaster();
var o:Object = new Object();

o.onFinish = function():void{
    trace("onFinish");
};
o.onStart = function():void{
    trace("onStart");
};
o.onChange = function(counter:int):void{
    trace("onChange " counter);
};
test.addListener(o);

test.start();


output

ActionScript-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
onStart
onChange 0
onChange 1
onChange 2
onChange 3
onChange 4
onChange 5
onChange 6
onChange 7
onChange 8
onChange 9
onFinish
Gänsefleisch ma ihr´n Hund online!?

Inder nett...
  • Go to the top of the page

public

Moderator

Posts: 546

Location: Dessau

2

Tuesday, February 5th 2008, 5:33am

und nun nur dr Vector Test

ActionScript-Quelltext

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
import de.djpublic.utils.*;

var v:Vector = new Vector();

v.addElement(1);
v.addElement(2);
v.addElement(3);
/*
*    false wenn 3 vorhanden, 3 wird hinzugefügt
*    true wenn 3 nicht vorhanden, 3 wird nicht hinzugefügt
*/
var bool:Boolean v.add(3);





var it:ListIterator = new ListIterator(v);

while(it.hasNext()){
    trace("index " it.cursor " = " it.next());
}

trace("index von element 2 = " v.getIndex(2));

var arr:Array = v.toArray();
trace(arr);


output

ActionScript-Quelltext

1
2
3
4
5
index 0 1
index 1 2
index 2 3
index von element 2 1
1,2,3
Gänsefleisch ma ihr´n Hund online!?

Inder nett...
  • Go to the top of the page