actionscript 3.0 built-in collision detection seems to be flawed even with perfect rectangles - actionscript

This is my first post. I hope the answer to this is not so obviously found- I could not find it.
I have a collision detection project in as3- I know that odd shapes will not hit the built-in detection methods perfectly, but supposedly perfect rectangles are exactly the shape of the bounding boxes they are contained it- yet- running the code below, I find that every once in a while a shape will not seem to trigger the test at the right time, and I cannot figure out why.
I have below two classes- one creates a rectangle shape, and a main class which creates a shape with random width and height, animates them from the top of the screen at a random x value towards the bottom at a set rate, mimicking gravity. If a shape hits the bottom of the screen, it situates itself half way between the displayed and undisplayed portions of the stage about its lower boundary, as expected- but when two shapes eventually collide, the expected behavior does not always happen- the expected behavior being that the shape that has fallen and collided with another shape should stop and rest on the top of the shape it has made contact with, whereas sometimes the falling shape will fall partially or completely through the shape it should have collided with.
does anyone have any idea why this is?
here are my two classes below in their entirety:
// box class //
package
{
import flash.display.Sprite;
public class Box extends Sprite
{
private var w:Number;
private var h:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public function Box(width:Number=50,
height:Number=50,
color:uint=0xff0000)
{
w = width;
h = height;
this.color = color;
init();
}
public function init():void{
graphics.beginFill(color);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
}
}
}
//main class//
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Boxes extends Sprite
{
private var box:Box;
private var boxes:Array;
private var gravity:Number = 16;
public function Boxes()
{
init();
}
private function init():void
{
boxes = new Array();
createBox();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
box.vy += gravity;
box.y += box.vy;
if(box.y + box.height / 2 > stage.stageHeight)
{
box.y = stage.stageHeight - box.height / 2;
createBox();
}
for(var i:uint = 0; i < boxes.length; i++)
{
if(box != boxes[i] && box.hitTestObject(boxes[i]))
{
box.y = boxes[i].y - box.height;
createBox();
}
}
}
private function createBox():void
{
box = new Box(Math.random() * 40 + 10,
Math.random() * 40 + 10,
0xffaabb)
box.x = Math.random() *stage.stageWidth;
addChild(box);
boxes.push(box);
}
}
}

Make sure box.vy never exceeds any of the heights of any boxes created. Otherwise, it is possible the box can pass through other boxes while falling. (if box.vy = 40 and boxes[i].height=30, it is possible to pass right over it).
Just add a check:
if(box.vy>terminalVelocity)box.vy=terminalVelocity)
Where terminalVelocity is whatever the minimum height a box can be (in your code, it looks like 10). If you really want those small boxes, you will have to use something more precise than hitTestObject.

Related

Graphics drawing code generates blank images, only on iOS

My app displays some images that I created using Image.createImage(). In some cases, the images are completely blank, but only on iOS. The images work fine on Android. Also, I create several images using Image.createImage() and most of them work fine. I don't see any difference between those and these.
To reproduce, run the enclosed app on both Android and iOS. The app shows two images. The second one is taken from the bottom half of the first one. On Android, the images show up fine. On iOS, the images show up for a few seconds, then vanish. It turns out that they only show up while iOS is displaying the startup screen. Once it switches to the actual app, the images are blank, although they take up the same space. Further tests reveal that the images are the correct size but are filled with transparent pixels.
I should say that, in my actual application, the images scale with the size of the screen, and are colored according to a user preference, so I can't just load them from a resource.
(BTW Notice the change I made to the stop method. This is unrelated but worth mentioning.)
Here's the test case:
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Graphics;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.util.Arrays;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
#SuppressWarnings("unused")
public class HalfImageBug {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
}
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Hi World", new BorderLayout());
hi.addComponent(BorderLayout.CENTER, makeComponent());
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
// This was originally if, but it should be while, in case there are multiple layers of dialogs.
while (current instanceof Dialog) {
((Dialog) current).dispose();
current = Display.getInstance().getCurrent();
}
}
public void destroy() {
}
private Component makeComponent() {
final Container container = new Container(new BoxLayout(BoxLayout.Y_AXIS));
container.setScrollableY(true);
container.add(new Label("Full Image:"));
Image fullIcon = createFullImage(0x44ff00, 40, 30);
Label fullImage = new Label(fullIcon);
container.add(fullImage);
container.add(new Label("---"));
container.add(new Label("Half Image:"));
Image halfIcon = createHalfSizeImage(fullIcon);
Label halfImage = new Label(halfIcon);
container.add(halfImage);
return container;
}
private Image createFullImage(int color, int verticalDiameter, int horizontalRadius) {
// Make sure it's an even number. Otherwise the half image will have its right and left halves reversed!
int diameter = (verticalDiameter / 2) * 2;
final int iconWidth = 2 * horizontalRadius;
int imageWidth = iconWidth + 2;
int imageHt = diameter + 2;
Image fullImage = Image.createImage(imageWidth, imageHt);
Graphics g = fullImage.getGraphics();
g.setAntiAliased(true);
g.setColor(color);
g.fillRect(0, 0, imageWidth, imageHt);
g.setColor(darken(color, 25));
g.fillArc(1, 1, iconWidth, diameter, 180, 360);
g.setColor(0xbfbfbf);
final int smallerHt = (9 * diameter) / 10;
g.fillArc(0, 0, iconWidth, smallerHt, 180, 360);
Image maskImage = Image.createImage(imageWidth, imageHt);
g = maskImage.getGraphics();
g.setAntiAliased(true);
g.setColor(0);
g.fillRect(0, 0, imageWidth, imageHt);
g.setColor(0xFF);
g.fillArc(1, 1, iconWidth, diameter, 180, 360);
fullImage = fullImage.applyMask(maskImage.createMask());
return fullImage;
}
private Image createHalfSizeImage(Image fullImage) {
int imageWidth = fullImage.getWidth();
int imageHt = fullImage.getHeight();
int[] rgbValues = fullImage.getRGB();
// yeah, I've since discovered a much more sensible way to do this, but it doesn't fix the bug.
int[] bottomHalf = Arrays.copyOfRange(rgbValues, rgbValues.length / 2, rgbValues.length);
//noinspection StringConcatenation
Log.p("Cutting side image from " + imageWidth + " x " + imageHt + " to " + imageWidth + " x " + (imageHt / 2));
return Image.createImage(bottomHalf, imageWidth, imageHt / 2);
}
private static int darken(int color, int percent) {
if ((percent > 100) || (percent < 0)) {
throw new IllegalArgumentException("Percent out of range: " + percent);
}
int percentRemaining = 100 - percent;
return (darkenPrimary((color & 0xFF0000) >> 16, percentRemaining) << 16)
| (darkenPrimary((color & 0xFF00) >> 8, percentRemaining) << 8)
| (darkenPrimary(color & 0xFF, percentRemaining));
}
private static int darkenPrimary(int primaryValue, int percentRemaining) {
if ((primaryValue < 0) || (primaryValue > 255)) {
throw new IllegalArgumentException("Primary value out of range (0-255): " + primaryValue);
}
return (primaryValue * percentRemaining) / 100;
}
}
This is discussed in this issue.
Generally the images initially appear because of the screenshot process that shows them so they never really show up on iOS natively.
A common cause for these issues is creating images off of the EDT which doesn't seem to be the issue in this specific code.
It's hard to see what is going on so I guess we'll need to evaluate the issue.
Here's a workaround. This works, but doesn't anti-alias very well. It will do until the iOS code gets fixed.
The problem is as described elsewhere. The Graphics.fillArc() and drawArc() methods work fine on Android, but often fail on iOS. Here's the behavior:
if width == height, they correctly draw a circle.
if width < height, they should draw an ellipse, but they draws a circle, centered over the intended ellipse, with a diameter equal to width.
if width > height, they draw nothing.
The workaround draws a circle against a transparent background, then draws that circle, squeezed in one direction to an ellipse, into the proper place. It doesn't do a very good job of anti-aliasing, so this is not a good substitute for working code, but it will do until the bug gets fixed. (This workaround handles fillArc, but it shouldn't be hard to modify it for drawArc()
/**
* Workaround for fillArc bug. Graphics.fillArc() works fine on android, but usually fails on iOS. There are three
* cases for its behavior.
*
* If width < height, it draws a circle with a diameter equal to width, and concentric with the intended ellipse.<br>
* If width > height, it draws nothing.<br>
* If width == height, it works correctly.
*
* To work around this we create a separate image, draw a circle, re-proportion it to the proper ellipse, then draw
* it to the Graphics object. It doesn't anti-alias very well.
*/
public static void fillArcWorkaround(Graphics masterG, int x, int y, int width, int height, int startAngle, int arcAngle) {
if (width == height) {
masterG.fillArc(x, y, width, height, startAngle, arcAngle);
} else {
int max = Math.max(width, height);
Image tempCircle = Image.createImage(max, max);
Graphics tempG = tempCircle.getGraphics();
tempG.setColor(masterG.getColor());
tempG.fillRect(0, 0, max, max);
// At this point tempCircle is just a colored rectangle. It becomes a circle when we apply the circle mask. The
// region outside the circle becomes transparent that way.
Image mask = Image.createImage(max, max);
tempG = mask.getGraphics();
tempG.setAntiAliased(masterG.isAntiAliased());
tempG.setColor(0);
tempG.fillRect(0, 0, max, max);
tempG.setColor(0xFF); // blue
tempG.fillArc(0, 0, max, max, startAngle, arcAngle);
tempCircle = tempCircle.applyMask(mask.createMask());
// Now tempCircle is a filled circle of the correct color. We now draw it in its intended proportions.
masterG.setAntiAliased(true);
masterG.drawImage(tempCircle, x, y, width, height);
}
}

gotoAndPlay not finding Frame Label AS3

I'm trying to build a simple Flash game where the user drags a sombrero onto a cactus. I've got it so that when you drag the sombrero anywhere but the cactus, it snaps back to it's original position. I had it so when you drag it onto the cactus, it stays there.
What I want is when the user drags the sombrero onto the cactus, it takes you to a screen that says "YAY! Play again?" I put a gotoAndPlay() inside my if statement:
if(dropTarget.parent.name == "cactus")
{
//scaleX = scaleY = 0.2;
//alpha = 0.2;
//y = stage.stageHeight - height - -100;
//buttonMode = false;
//removeEventListener(MouseEvent.MOUSE_DOWN, down);
gotoAndPlay("playAgain");
trace("dropped on cactus");
}
else
{
returnToOriginalPosition();
}
I labeled my second frame as "playAgain." I get an error saying:
ArgumentError: Error #2109: Frame label playAgain not found in scene playAgain.
at flash.display::MovieClip/gotoAndPlay()
at net.dndgtal.Cactus_Game::sombrero/stageUp()
I have Googled and checked and double checked all the suggestions, but cannot get it to work. I don't have a scene "playAgain," only "scene1." I've tried specifying both the scene and the frame- that doesn't work either. And I tried just putting in gotoAndPlay(2), for frame two, but that just does nothing.
Am I missing something? Any help would be appreciated. Here is all of my code if that helps:
package net.dndgtal.Cactus_Game
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Point;
public class sombrero extends MovieClip
{
protected var OriginalPosition:Point;
public function sombrero ()
{
OriginalPosition = new Point(x, y);
buttonMode = true;
addEventListener ( MouseEvent.MOUSE_DOWN, down );
//trace("sombrero constructor");
}
protected function down (event:MouseEvent):void
{
parent.addChild(this);
startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, stageUp);
//trace("DOWN");
}
protected function stageUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, stageUp);
stopDrag();
if (dropTarget)
{
if(dropTarget.parent.name == "cactus")
{
//scaleX = scaleY = 0.2;
//alpha = 0.2;
//y = stage.stageHeight - height - -100;
//buttonMode = false;
//removeEventListener(MouseEvent.MOUSE_DOWN, down);
gotoAndPlay("playAgain");
trace("dropped on cactus");
}
else
{
returnToOriginalPosition();
}
}
else
{
returnToOriginalPosition();
}
}
protected function returnToOriginalPosition(): void
{
x = OriginalPosition.x;
y = OriginalPosition.y;
}
}
}
Thanks! Let me know if you have any questions.
Your issue is likely one of scope. When you use gotoAndPlay("playAgain"), it will take the current timeline (scope), which is the sombrero class, and attempt to find a frame label or scene called "playAgain" on it's timeline.
Presumably, you want the main timeline to gotoAndPlay the frame label "playAgain", not the sombrero.
To access the main timeline, you can use MovieClip(root).gotoAndPlay("playAgain").
If it's not the main timeline, but the parent of sombrero, you can access it with MovieClip(parent).gotoAndPlay("playAgain")
If the relationship between the sombrero and whichever timeline your trying to advance is more complicated than that, then you could pass in a reference to it in your sombrero constructor:
private var targetTimeline:MovieClip;
public function sombrero (targetTimeline_:MovieClip)
{
targetTimeline = targetTimeline_;
//...rest of constructor code
And then:
if(dropTarget.parent.name == "cactus"){
targetTimeline.gotoAndPlay("playAgain");
And when you create the sombrero, pass in the target timeline:
var hat:sombrero = new sombrero(target);

how to make alpha part of movieClip non-clickable (actionscript starling)?

I am using Starling.
I can't figure out how to make alpha part of bitmap non-clickable - say I have a rabbit in center and alpha around.
first i embed the bitmap:
[Embed(source="assets/stuff.xml", mimeType="application/octet-stream")]public static const Axml:Class;
[Embed(source="assets/stuff.png")]public static const Apng:Class;
Then create the bitmap atlass (spritesheet):
var tx1:Texture = Texture.fromBitmap(new Apng());
var xm1:XML = XML(new Axml());
var atlas:TextureAtlas = new TextureAtlas(tx1, xm1);
I made a MovieClip and add bitmap to it like that:
var movie:MovieClip = new MovieClip(atlas.getTextures("rabbit_run_"), 18);
addChild(movie);
movie.play();
starling.core.Starling.juggler.add(movie);
but when I click on it even on alpha part movieClip becomes "clicked".
I only need rabbit itself to be clickable, not around it!
How to make alpha non-clickable? Thanks
1: You cannot make image clickable in one area and nonclickable in another. But you can get color of pixel under mouse and ignore ckicks over transparent pixels
import flash.events.MouseEvent;
import flash.display.BitmapData;
var rabbitBitmap:BitmapData = new BitmapData(rabbit.width, rabbit.height, true, 0xFFFFFFFF);
rabbitBitmap.draw(rabbit);
addEventListener(MouseEvent.MOUSE_MOVE, mmh)
rabbit.addEventListener(MouseEvent.CLICK, mch)
rabbit.buttonMode = true;
function mmh(e:MouseEvent):void {
rabbit.useHandCursor = getAlpha() != 255;
}
function mch(e:MouseEvent):void {
if (getAlpha() != 255) {
// ... do someth
}
}
function getAlpha ():int {
var rgba:uint = rabbitBitmap.getPixel32(rabbit.mouseX, rabbit.mouseY);
return rgba & 0xff;
}
2: Use vector graphics for your rabbit :)

Newly loaded sprites not behaving properly in XNA 4.0

Here is the code for a project im working on, where an enemy moves back and forth at the bottom of the screen.
class enemy1
{
Texture2D texture;
public Vector2 position;
bool isAlive = false;
Random rand;
int whichSide;
public enemy1(Texture2D texture, Vector2 position)
{
this.texture = texture;
this.position = position;
}
public void Update()
{
if (isAlive)
{
if (whichSide == 1)
{
position.X += 4;
if (position.X > 1000 + texture.Width)
isAlive = false;
}
if (whichSide == 2)
{
position.X -= 4;
if (position.X < 0)
isAlive = false;
}
}
else
{
rand = new Random();
whichSide = rand.Next(1, 3);
SetInStartPosition();
}
}
private void SetInStartPosition()
{
isAlive = true;
if (whichSide == 1)
position = new Vector2(0 - texture.Width, 563 - texture.Height);
if (whichSide == 2)
position = new Vector2(1000 + texture.Width, 563 - texture.Height);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
Now i want there to be a few enemys going back and forth but they start at differant positions so it looks like there is a few enemys going back and forth at the bottom of the screen. I have managed to draw a few other enemies on the screen, except they do not behave like the first enemy. They just are pictures on a screen not moving anywhere. So now all i have is the hero moving around and one enemy at the bottom of the screen, along with 5 other enemys sitting at the top of the screen doing nothing. How do i easily add a new sprite from a class that has the same behavior, at any time, while not making a billion variables to store them in?
Generally it's a good idea to have similar logic contained within the proper class, so if all Sprites where to do the same thing, then all you would need to do is put your movement code inside a public method and then call that method inside Update().
So, if your Sprite class looks something like this:
public class Sprite
{
private Vector2 Position;
public Sprite(Texture2D texture, Vector2 position)
{
Position = position;
}
//then just add this
public void MoveSprite(int amount)
{
position.X += amount;
}
}
Now, the object name "Sprite" is pretty generic, you will more than likely have many "Sprites" in your game.
So you're going to want to follow good OOP practices and maybe name this specific sprite something different and then have it derive from this class we're looking at right now. (But i'm not going to make design decisions for you)
This was a vague question, but that's my best shot at an answer for you.

randomness from spritemap in spritebatch

Im sure this is easily fixed, and I do have searched both high and low, traversed the net both east, west, north and south but to no prevail...
My problem is this. Im in the middle of trying to make a bejeweled clone, just to get me started in xna. However im stuck on the random plotting of gems/icons/pictures.
This is what i have.
First a generated list of positions, a random and a rectangle:
List<Vector2> platser = new List<Vector2>();
Random slump = new Random();
Rectangle bildsourcen;
protected override void Initialize()
{
for (int j = 0; j < 5; j++)
{
for (int i = 0; i < 5; i++)
{
platser.Add(new Vector2((i*100),(j*100)));
}
}
base.Initialize();
}
Pretty straight-forward.
I also have loaded a texture, with 5 icons/gems/pictures -> 5*100px = width of 500px.
allImage = Content.Load<Texture2D>("icons/all");
Then comes the "error".
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
int x = slump.Next(2);
bildsourcen = new Rectangle((x * 100), 0, 100, 100);
for (int i = 0; i < platser.Count; i++)
{
spriteBatch.Draw(allImage, new Rectangle((int)platser[i].X, (int)platser[i].Y, 100, 100), bildsourcen, Color.White);
}
So, there is my code. And this is what happens:
I want it to randomly pick a part of my image and plot it at the given coords taken from the vector2-list. However, it puts the same image at all coords and keeps randomly replacing them, not with random images but with the same. So the whole board keeps flickering the same icons. Ie, instead of generating 15231 and keeping it frozen, it one second puts 11111 and the next second it puts 33333.
Does anybody understand what im trying to describe ? I'm almost at the point of pulling my own hair out. The cat's hair has already been pulled...
Thx in advance
The Draw function is called once each frame. This line:
int x = slump.Next(2);
Is generating a random number (either a 0 or a 1 in this case) each frame, hence the flicker.
The line after that selects a sprite from your sprite atlas based on that number (specifically it specifies the rectangle containing that sprite). And in the loop that follows you're drawing multiple copies of that sprite (always the same image).
You should be doing all of your game logic in your Update function. That function will give you a time and you will probably want to implement a method of waiting for a certain amount of time to pass before you generate a random block (so keep accumulating the time that passes between each Update, until it reaches some threshold). The exact mechanics of when you want to generate your random block is up to you.
Of course, that is not to mention that there are other flaws in the structure of your code. Bejewelled is played on a fixed-sized board with different coloured blocks (each block you could represent with a number from 1 to X). The location of the blocks should be be implicit in your data structure (so you don't need to generate your platser list).
So your Game class should have something like:
const int BoardWidth = 10;
const int BoardHeight = 10;
int[,] board = new int[BoardWidth, BoardHeight];
Then in your Initialize function you should fill board and perhaps use 0 as an empty space and 1 to X to represent your colours, like so:
for(int x = 0; x < BoardWidth; x++) for(int y = 0; y < BoardHeight; y++)
{
board[x,y] = slump.Next(1, 6); // gives 5 different sprites
}
Then in Update wait for user input or a time-out before modifying the board (depending on your gameplay).
Then in your Draw function do something like this:
for(int x = 0; x < BoardWidth; x++) for(int y = 0; y < BoardHeight; y++)
{
if(board[x,y] == 0) continue; // don't render an empty space
Vector2 position = new Vector2(100*x, 100*y);
Rectangle bildsourcen = new Rectangle(100*(board[x,y]-1), 0, 100, 100);
sb.Draw(allImage, position, bildsourcen, Color.White);
}

Resources