zqf

Members
  • Content count

    27
  • Joined

  • Last visited

Community Reputation

388 Neutral

About zqf

  • Rank
    Member
  1. I'm sure someone else would have something more elegant but this is some code I dug up out of an old project of my own for what sounds like a similar game, so it does work :p // This rectangle defines your screen size. Replace 640x480 with the size of your Flash app. // We will spawn enemies just outside this rectangle. var _screenRect:Rectangle = new Rectangle(0, 0, 640, 480); // This rectangle defines the range of positions enemies should spawn in. It will be adjusted // to match the size of the enemy being spawned during the set position method. var _spawnRect:Rectangle = new Rectangle(); // Return a random number between min and max. private function RandomWithinRange(min:Number, max:Number):Number { return Math.random() * (max - min) + min; } // Send a sprite class to this to move it off the screen. private function MoveOffscreen(enemy:Sprite):void { var spawnX:Number; var spawnY:Number; // We set the spawn rectangle to the size of the screen but // expand it out by the size of the object being spawned. // This will allow enemies of any size to always be moved off screen. var padX:Number = enemy.width; var padY:Number = enemy.height; _spawnRect.x = _screenRect.x - padX; _spawnRect.width = _screenRect.width + padX; _spawnRect.y = _screenRect.y - padY; _spawnRect.height = _screenRect.height + padY; // Pick randomly between X and Y, and move the spawn position to the full extent on // that axis. Then randomise the other axis. if (Math.random() > 0.5) { // We are setting the X to its full distance on the left or right, randomly pick which if (Math.random() > 0.5) { // left spawnX = _spawnRect.x; } else { // right spawnX = _spawnRect.width; } // Randomise the other axis. spawnY = RandomWithinRange(_spawnRect.y, _spawnRect.height); } else { // We are setting the Y to its full distance up or down, randomly pick which if (Math.random() > 0.5) { // Up spawnY = _spawnRect.y; } else { // down spawnY = _spawnRect.height; } // Randomise the other axis. spawnX = RandomWithinRange(_spawnRect.x, _spawnRect.width); } // Move the enemy to the new position enemy.x = spawnX; enemy.y = spawnY; }
  2. In a component based system how should I give sub-components references that their parent shouldn't have? eg: An Entity: maybe a monster, creates a Ray attack class for itself. The Ray Attack: This will cast the ray and receive the hit callbacks from the world, calculating the nearest hit (or more if the attack can penetrate) and hitting them. The World: which in my case at the moment is a box2D simulation. I want the child object, the ray attack to have access to the world (or more specifically, access to a single world method: 'QueryRay'. I don't want the parent entity to however. Should I pass it into some controller that will call a setup method on the ray attack that gives it the reference it needs perhaps...? At the moment my game passes around a 'services' class, containing interfaces to sub-controllers. This defines the capacity entities have to act outside of themselves. One of these is a 'rayAttack' controller that handles EVERY ray attack from anything, with the highly elegant: var rayAttack = services.rayAttack; rayAttack.Setup(a whole bunch of params); rayAttack.Fire(x0, y0, x1, y1); This is one example, I have all these central controllers bloating up with specific methods and I'd like to turn them into components but keep references tight to where they're needed only.
  3. Flash Blitting Flicker

    Cleaned up a bit here it is. Language is Actionscript 3, only libraries used are Box2D for physics/collision, the rest is my own or standard Flash stuff. // On Frame draw call in Main public function Render():void { canvas.lock(); canvas.fillRect(canvas.rect, 0xff000000); _manager.currentState.Render(canvas); canvas.unlock(); } // In GameState public function Render(canvas:BitmapData):void { var mapRendPoint:Point = _gameModel.mapRenderPoint; // Position is taken from the box2D physics position of a camera entity tracking the player. // Subtract half screen width to get the top left point of the screen. var scrX:Number = _gameModel.camera.position.x * Main.PIXELS_PER_METRE - screenInfo.halfWidth; var scrY:Number = _gameModel.camera.position.y * Main.PIXELS_PER_METRE - screenInfo.halfHeight; scrX = Math.round(scrX); scrY = Math.round(scrY); mapRenderPoint.x = scrX; mapRenderPoint.y = scrY; // Render point is kept within the bounds of the tile map _gameModel.TileMap.CapRenderPoint(mapRenderPoint); // TileMap is given map position and adds every Tile to be drawn to the Draw List. _gameModel.TileMap.Render(mapRenderPoint); /* Game objects are added to the list */ // display list is called, this will actually run the calls to copy to canvas. draw list is then wiped _blitter.RenderFrame(canvas, mapRenderPoint); } // In Tile class, called from _blitter after TileMap has added all the tiles to be drawn public function Render(canvas:BitmapData, topLeft:Point):void {     // Translate to screen position     _rendPosition.x = _position.x - topLeft.x;     _rendPosition.y = _position.y - topLeft.y;     // Draw     canvas.copyPixels(_curFrame, _rendRect, _rendPosition, null, null, true); }
  4. Flash Blitting Flicker

    Edit: Sorry I meant to post this in Beginners forum. I am creating a platformer where the entire game is blitted. The tile map seemed to be fine, however certain tiles with transparent pixels flicker very badly when the screen moves, appearing to change brightness rapidly, whilst other parts remain okay (I think). Please check out this little demo to see the effect (use WASD to move around). http://www.dampdogdev.com/devDemos/rngFlicker/flicker.html I've tried rounding the exact render position, flooring it, and double buffering but nothing I've tried seems to affect it.
  5. I dunno if this is helpful, and maybe this is covered by code you've removed to make your example smaller but an observation: Your renderer is defaulting to using the screen canvas as a source... public function Renderer(bitmapdata) { mBitmapData = bitmapdata; this.mDisplayBD = bitmapdata; ...If your Loader doesn't complete and replace the source then it will be copying the canvas onto itself and there's no load failure listener so if it fails you won't know about it.
  6. Hello there. This question is about a platform game in box2D but I assume that it would be applicable to any platform game. At the moment my character jumps by setting his vertical velocity, but I'm at a point where I want to tweak the gravity of the game but not affect the actual distance the player will jump, so I can adjust the feel of the gravity without constantly have to alter the player jump strength. Gravity force vector in box2D is currently +22 on y. I want the player body to be pushed upward exactly 3 metres. The player can double jump, and I want this to happen regardless of what the current velocity of the player is. If the player is falling very quickly, hitting jump will cause them to stop and pop up 3 metres. Thanks
  7. I understand the desire to keeping game actors from seeing these large data structures, but I'm still trying to see what would be considered a safe way to bring them together? Is this where it is simplest to just make position and collision shape public variables or getters? Using movement and collision as an example still: The world loops through all actors, sends them to a collision class that can move the actor and call them when collisions occur? The collision class doesn't see all of the world, and can see the required details of the actor it's moving? In this way objects don't control exactly how they are moved, or see any world information, they just have a velocity and a shape, and methods for responding to a collision.
  8. Thanks for your replies. I'm mainly just paranoid about getting into bad habits
  9. How about the world holding a list of movers that it calls when it updates? Objects that want to move would add themselves to this list, that sort of thing?
  10. What about the actor sending itself to some World interface to be moved during it's Update() (like World.MoveActor(this)) ?
  11. Another newbie here finding it hard to grasp these object oriented principles. I think I understand the core idea but extending it to practical applications leaves me confused everytime as to what data should be public or private, or where interfaces should be etc: As an example, a platform game. When an actor moves it needs to find the actors and tiles nearby to collide with (stored in a World object say). When it collides it needs to know the exact position of the other actor, plus what kind of shape it is, and how the collision will be resolved (moving the shape back maybe). How should access to this information be handled? Where should the code that is calculating the collision be contained in and run from? Sorry if these questions sound dumb but this stuff leaves me completely lost :/
  12. I can't remember where I found the source for this, but I converted this algorithm from C to actionscript and it works perfectly, giving me the position of the hit if one has occurred. However I'd like to extend it so it can give me the specific side that the ray hit, so I can use it for bouncing objects off. Is this possible with this algorithm (and if so how) or would it be better to use a different technique? [CODE] public static function LineSegmentVsTile(v1:LineSeg, AABB:ATile, impactPoint:Point):Boolean { var dirFracX:Number = 1 / v1.vx; var dirFracY:Number = 1 / v1.vy; var t1:Number = (AABB.position.x - v1.a.x) * dirFracX; var t2:Number = ((AABB.position.x + (AABB.halfWidth * 2)) - v1.a.x) * dirFracX; var t3:Number = (AABB.position.y - v1.a.y) * dirFracY; var t4:Number = ((AABB.position.y + (AABB.halfHeight * 2)) - v1.a.y) * dirFracY; var tmin:Number = Math.Max(Math.Min(t1, t2), Math.Min(t3, t4)); var tmax:Number = Math.Min(Math.Max(t1, t2), Math.Max(t3, t4)); if (tmax < 0) { return false; } else if (tmin > 1) { return false; } else if (tmin > tmax) { return false; } else { if (impactPoint) { var colX:Number = v1.vx * tmin; var colY:Number = v1.vy * tmin; impactPoint.x = v1.a.x + colX; impactPoint.y = v1.a.y + colY; } return true; } } [/CODE] Thanks
  13. At the moment I have incredibly basic AI and updating for monster movement that all acts on a single movement vector, which is pretty awful. AI just directly alters the movement vector, and in order to allow the monster to move around things, the AI sets the vector every few frames So if I apply a push force from an attack, the monster will move backward until it's next AI update and then it will just snap back to it's own walk vector, it won't push against the force acting on it or anything, it just overrides it. I would like to split it up so that a force vector and an AI Vector are combined to produce a final vector used during update to move it. Things for it to consider: 1: Moving toward its attack target (if it has one) 2: Moving away from fellow monsters it is touching 3: Following the side of walls when it encounters them, attempting to move round them 4: Being pushed back by player attacks, explosions, or environmental effects like magnets which have specific strengths. The first three are under AI control, so it's speed is capped to the movement speed of the monster. However, the last, external forces, shouldn't be capped, as it could be blown across a room if a force is high enough. I don't know how I could combine these two, so when the mob's own movement vector is applied it doesn't go over the cap but if it has been pushed externally it isn't capped. Help on how I can combine these would be really appreciated.
  14. Large objects in a uniform grid

    [quote]put bosses higher up in the tree and traverse the tree from the top[/quote] I can see how that works for object vs object, but how would that work for Ray tracing? The Ray is moving through the lowest grid so needs to find any solid objects on that layer
  15. I've fairly successfully created a Grid structure of linked lists for object vs object interaction, which I expanded up to include Ray vs Object and Ray vs Tile Intersection. But I'd like to have larger objects. Quadtrees are rather beyond my understanding at the moment (and trees in general), so I was considering two alternatives: > Additional larger Grids. At the moment the grid is 32x32 which matches the Tile grid, so a second grid of 64x64 and perhaps a third at 128x128 would cover a large set of object sizes. My main issue with extra grids however is how to handle Ray querys vs two or perhaps three grids at a time, as well as the general code of checking up and down the list. > A parent/Child system where large objects are divided into smaller children, and these children sit in the Grid, moving with their parent and calling them if they collide with something. This technique would presumably have a lot of overhead if a huge object is moving around, as it has to remove and insert many children, but on the other hand it could handle almost any size, particularly giant obects which would be used rarely anyway (for example, huge bosses). And handling of Ray queries would barely change. I'm more inclined to go with the second option but perhaps I'm missing something bad about the group of little objects idea. Thoughts/suggestions?