Welcome to the monastery iraid3r
Anonym pointend you about the fact that $enem is not an object, but maybe this is good for you: if i understand your question correctly you create an object-sprite and want to reuse for many anonymous hashes like $enem (so $enem and company will be not objects).
In this case you have a lot of options: if you isolate,for example, coordinates for the creation of the army into an ArrayofArrays where each element is a pair of x and y. like @coord_pairs = ( [1,2], [3,7], [5,9]) then you can cycle through them as in:
my @coord_pairs = ( [1,2], [3,7], [5,9]);
my @enemy_container;
foreach my $index (coord_pairs) {
my $enem = {
sprite => $enemy_sprite ,
v_y => $coord_pairs[$index]->[0], # element 0 of the current p
+air
v_x => $coord_pairs[$index]->[1], # element 1 of the current p
+air
};
push @enemy_container, $enem;
}
As said there a lot of options to deal with lists and arrays: foreach while map shift pop ...
i know nothing about SDL, and dunno if you can reuse the created sprite seveveral times: maybe you need to clone it, or in the worst case you can create a new sprite for each enemy created, even if the png is the same:
my @coord_pairs = ( [1,2], [3,7], [5,9]);
my @enemy_container;
foreach my $index (coord_pairs) {
my $temp_enemy_sprite = SDLx::Sprite->new ( width => $20, height =>
+ $20 );
$enemy_sprite->load('example.png');
my $enem = {
sprite => $temp_enemy_sprite ,
v_y => $coord_pairs[$index]->[0], # element 0 of the current p
+air
v_x => $coord_pairs[$index]->[1], # element 1 of the current p
+air
};
push @enemy_container, $enem;
}
As little side note, avoid name like $20 for your variables: if you are starting coding choice meaningful name for them as i tried to do in my little example. There is chance to other parts of the program change the value (accidentally) of $20 ending with $20 containing 30.
Perl let you to define true constant as described in the docs. In this case you can use constant TWENTY => 20; because number are not allowed as constant name (but you can use _20).
Post clear question to obtain the best from perlmonks. i'm courious to see your gaming attempt anyway.
HtH L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
| [reply] [d/l] [select] |
$enem is not an object
Here are ten sprites
my @sprites = map {
my $es = SDLx::Sprite->new;
$es->load('example.png');
$es;
} 1 .. 10;
| [reply] [d/l] |