A Flash Developer Resource Site

# Thread: How to make combinable objects

1. ## How to make combinable objects

This question is not specific to a game, I want to know what is the best way to make combinable objects.

Say I have 3 objects A,B,C, if i combine
A+B = AB
A+C = AC
B+C = BC

But if I had 4 objects, the number of possible combinations increase even more( limited to 2 objects per combinations)! Now what if I had 50 objects? The number of combinations possible just goes beyond what I can hardcode in the game, so is there any possible formula such that the combinations are automatically calculated? This migh be really simple, but i can't think of a very innovative way to handle this....please help!

2. can you use this method (testing with an array of "strings" in AS2)
PHP Code:
``` tmp = []; arr = ["A","B","C","D","E","F"]; len = arr.length; for(var n=0;n!=len;n++){ for(var m=0;m!=len;m++){ if(arr[n]!=arr[m]){ tmp.push(arr[n]+arr[m]); } } } trace(tmp); /* - output - AB,AC,AD,AE,AF, BA,BC,BD,BE,BF, CA,CB,CD,CE,CF, DA,DB,DC,DE,DF, EA,EB,EC,ED,EF, FA,FB,FC,FD,FE */  ```

3. hmm, thanks, but what about the duplicates? AB is same as BA!

4. Code:
```tmp = [];
arr = ["A","B","C","D","E","F"];
len = arr.length;

for(var n=0;n!=len;n++){
for(var m=n;m!=len;m++){
if(arr[n]!=arr[m]){
tmp.push(arr[n]+arr[m]);
}
}
}
trace(tmp);
/*
AF,BC,BD,BE,
BF,CD,CE,CF,
DE,DF,EF
*/```
Start the inner loop at the outer loops index.

5. Originally Posted by ImprisonedPride
/*
AF,BC,BD,BE,
BF,CD,CE,CF,
DE,DF,EF
*/

Start the inner loop at the outer loops index.
good catch
though in theory AB != BA

6. Thanks guys! The first part is solved!

Next is how should I define properties to combine, but that won't be hard after this!

7. Code:
```var objA:Object = {n:"A", x:1, y:2, z:3};
var objB:Object = {n:"B", x:3, y:1, z:2};
var objC:Object = {n:"C", x:4, y:1, z:3};
var objD:Object = {n:"D", x:0, y:3, z:1};
var objE:Object = {n:"E", x:2, y:1, z:2};

Object.prototype.toString = function():String {
var out:String = "{";
for (var i in this) {
out +=i + ":" + this[i] +",";
}
out = out.substr(0, out.length-1);
out += "};";
return out;
}

function run():Void {
tmp = [];
arr = [objA, objB, objC, objD, objE];
len = arr.length;

for(var n=0;n!=len;n++){
for(var m=n;m!=len;m++){
if(arr[n]!=arr[m]){
tmp.push(combineObjects(arr[n], arr[m]));
}
}
}
trace(tmp);
}

function combineObjects(a:Object, b:Object):Object {
var out:Object = {};
for (var i in a) {
out[i] = a[i]+b[i];
}
return out;
}

run();
/*
{z:5,y:3,x:4,n:AB};,
{z:6,y:3,x:5,n:AC};,
{z:5,y:3,x:3,n:AE};,
{z:5,y:2,x:7,n:BC};,
{z:3,y:4,x:3,n:BD};,
{z:4,y:2,x:5,n:BE};,
{z:4,y:4,x:4,n:CD};,
{z:5,y:2,x:6,n:CE};,
{z:3,y:4,x:2,n:DE};
*/```

8. Thanks, but I didn't exactly mean that kind of properties! Anyway, I guess this solves this entire matter. Thanks again!

9. What DID you mean then?

10. umm, by properties I meant a set of rules that define an object....when two objects combine these rules or behaviours come together to create a new object!
Like say, i define a "plank" as something which is solid and on which you can stand upon, and a "wheel" as something which can move in a direction! When we combine these two we get a skateboard(sort of).... meaning a movable plank on which the player can stand!

So this is kindof like what you did, but with a set of rules, instead of simple attributes!

11. you can represent an object as a bit-array and use bitwise logic to combine attributes. so, let's say bit 1 is whether you can stand on an object, bit 2 is whether it moves left and bit 3 is whether it moves right.

001 //plank
110 //wheels

now a bitwise OR defines their combination of properties

111 //plank + wheels (001 | 110)

so let's say you're only going to have 32 different properties, then you can forget about the hassle of writing a bit-array class and just use uints. this same example, ActionScript looks like this:
Code:
```const STANDABLE:uint = 1; //001
const MOVES_LEFT:uint = 1 << 1; //010
const MOVES_RIGHT:uint = 1 << 2; //100

var plank:uint;
var wheels:uint;

//to illustrate setting the bits
plank  |= STANDABLE; //001
wheels |= MOVES_LEFT | MOVES_RIGHT; //110

var skateboard:uint = combine( plank, wheels ); //111
var canMoveLeft:Boolean = hasAttribute( skateboard, MOVES_LEFT ); //true!

public function combine( obj1:uint, obj2:uint ) : uint
{
return obj1 | obj2;
}

public function hasAttribute( object:uint, attribute:uint ) : Boolean
{
return ( object & attribute ) == attribute;
}```

12. Yeh, I am planning on using the same approach( i actually found this two days ago while looking up uses of bitwise operators).....but thanks for the example code!

It gets a little more difficult when you have a few opposing properties though, and then there's a thing about prioritization!

13. it's not really any more complicated in those cases. for mutually exclusive attributes, you store a bit mask and since you're working with uints, priority can be handled natively by the value of the attribute.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap

Click Here to Expand Forum to Full Width