Accessing two types in a For...Next loop or function
Blitz3D Forums/Blitz3D Programming/Accessing two types in a For...Next loop or function
| ||
Hi all, thanks a lot for the help so far. I'm still struggling with types and functions... Anyway, that's the question: Assuming that I have a type 'enemy' that can shoot, and a type 'bullet', as: shooting.enemy=new enemy shooting entity= blah... blah... blue.bullet=new bullet blue.entity=blah... blah.. And I want to access to both in the same function or For...Next loop, i.e. the one that creates the bullets: function createbullet() for blue.bullet=each bullet positionentity blue\entity, entityX(shooting\entity), entityY(shooting\entity),entityY(shooting\entity) ...blah... This will also allow me to check the entity distances without checking for collisions (assuming that the enemies can shoot each other), as: for blue.bullet = each bullet if entitydistance (blue\entity, shooting\entity) < 1 then ...blah... Is that possible (or at least something like that)? Thanks Paolo |
| ||
Yes... but remember that if you've just got ellipsoid collisions, checking distances is all Blitz is doing anyway. I don't think you'd really be saving any time doing it yourself. Code to make enemies fire would look something like this: ; in your main loop... For E.Enemy = each Enemy if (Rnd(0,100) < 10) FireEnemyGun(E) Next ; outside the main loop... Function FireEnemyGun(E.Enemy) ; Make a new bullet B.Bullet = new Bullet B\Entity = CopyEntity(BulletSprite) ; Copy the firing enemy's position and rotation PositionEntity(B\Entity, EntityX(E\Entity), EntityY(E\Entity), EntityZ(E\Entity)) RotateEntity(B\Entity, EntityPitch(E\Entity), EntityYaw(E\Entity), EntityRoll(E\Entity)) ; Move bullet away from enemy so it doesn't blow itself up MoveEntity(B\Entity, 0, 0, 1.1) End Function The code to move all bullets and check them against enemies would look like this: For B.Bullet = each Bullet MoveEntity(B, 0, 0, 0.1) ; Move bullet forwards on its Z axis For E.Enemy = each Enemy If (EntityDistance(B\Entity, E\Entity) < 1) ExplodeEnemy(E) FreeEntity(B\Entity) Delete(B) End If Next Next |