Collision doesn't work work if i drag too fast - actionscript-3

I'm searching for this all day but it gives me the answer-box2d and I'm just a BEGINNER, box2d is advance for me, i don't really understand it. Someone help me, any answer will be appreciated.
hero.addEventListener(TouchEvent.TOUCH_BEGIN, touchhero);
stage.addEventListener(TouchEvent.TOUCH_END, drophero);
var dragbound1:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
function touchhero(event:TouchEvent):void
{
hero.startTouchDrag(event.touchPointID, false, dragbound1);
stage.addEventListener(Event.ENTER_FRAME, gohero);
}
function drophero(event:TouchEvent):void
{
hero.stopTouchDrag(event.touchPointID);
}
function gohero(event:Event):void{
if(hero.hitTestObject(wall)){
//popup something
}
}

You should use pixel perfect collision detection. Firstly you still want to check if your MovieClip's bounding box collides with another (the same method you're currently using). This is done to save your game from constantly checking the collision in such detail. If this is the case you can now check for the pixel collision.
Here's a good tutorial on this: http://code.tutsplus.com/tutorials/pixel-level-collision-detection--active-10862

Related

Actionscript acting funny

I was watching a youtube tutorial about as3,I tried to copy the code to see how it works, but even though it worked for the guy in the video, it didn't work for me..
Video Link(update): https://www.youtube.com/watch?v=pKj1IgWJd-A&ab_channel=WaarithAbdul-Majeed
(short post update)I made this late at night and didn't really include a lot of stuff in the post about the problem, so pretty much the movie clip is supposed to follow the mouse but it doesn't even though I made the code for it.
The errors are about circle.x = content.mouseX;
circle.y = content.mouseY;
Flash keeps popping up "1120: Access of undefined property content."
Code:
addEventListener(Event.ENTER_FRAME,shipgo);
function shipgo(e:Event){
ship_b.x=mouseX;
ship_b.y=mouseY;
}
Try referencing the stage for its mouse coordinates:
stage.addEventListener( Event.ENTER_FRAME, shipgo );
function shipgo ( e:Event ) :void
{
ship_b.x = stage.mouseX;
ship_b.y = stage.mouseY;
}

AS3 tween object not working with .hitTestObject()

I am having a major problem in my new browser app.
Okay so I made game where different cubes (squares) spawn at the top of the screen and I use the Tween class to make them go down the screen and then disappear.
However I want to detect a collision when a cube hits the player (that is also a flying cube).
I tried everything, truly everything but it does not seem to work. The problematic thing is that when I remove the "Tween" function it does detect collision with the hitTestObject method but when I add the "Tween" line collision won't be detected anymore.
It looks like this:
function enemiesTimer (e:TimerEvent):void
{
newEnemy = new Enemy1();
layer2.addChild(newEnemy);
newEnemy.x = Math.random() * 700;
newEnemy.y = 10;
if (enemiesThere == 0)
{
enemiesThere = true;
player.addEventListener(Event.ENTER_FRAME, collisionDetection)
}
var Tween1:Tween = new Tween(newEnemy, "y", null, newEnemy.y, newEnemy.y+distance, movingTime, true);
}
And the collision detection part:
private function collisionDetection (e:Event):void
{
if (player.hitTestObject(newEnemy))
{
trace("aaa");
}
}
I am desperate for some information/help on the topic, it's been bugging me for days.
Thanks for your time, I would be very happy if someone could help me out^^
First, make sure the "newEnemy" instance and the "player" instance are within the same container. If they are not, their coordinate systems might not match up and could be the source of your problem.
Otherwise, you need to keep a reference to each enemy instance you create. It looks like you are only checking against a single "newEnemy" variable which is being overwritten every time you create a new enemy. This might be why you can successfully detect collision between the player and the most recent "enemy" instance.
So... you need a list of the enemies, you can use an Array for that.
private var enemyList:Array = [];
Every time you create an enemy, push it to the Array.
enemyList.push(newEnemy);
In your "collisionDetection" function, you need to loop through all of the enemies and check if the player is touching any of them.
for(var i:int = 0; i < enemyList.length; i++)
{
var enemy = enemies[i];
if (player.hitTestObject(enemy))
{
trace("Collision Detected!");
enemy.parent.removeChild(enemy); // remove the enemy from the stage
enemies.splice(i, 1); // remove the enemy from the list
}
}
I'd suggest that you move to TweenMax, it just might solve your problem, and in my experience it's much better in every possible way.
Scroll down the following page to see a few variations of this library, I myself use TweenNano, they're completely free of charge:
https://greensock.com/gsap-as
I think some plugins cost money, but I doubt you'll ever need them.

FlxG.collide (with setTileProperties) colliding outside the tile space in flixel

so I have been using flixel I some recently, and I think I am getting a feel for it, but I have come across a issue.
I have a Map class, that uses the loadMap() function, which comes out great.
I then use setTileProperties() on my Water tile, that then calls a function, calls a Boolean on my player class (to say that he is in the water), which then slows him down. This still works great, except for one thing. When I pass the water tile above or to the right (about 10-16ish pixels, haven't gotten the exact number) outside the tile, it still slows the player. I am not sure if this is just the way that FlxG.collide() works, or if there is something I can do to fix it, or maybe if I should find another way to use the collision. All help is appreciated.
Here is the code, if you need it:
Map.as
private function createMap():void {
loadMap(new currentMap, currentTiles, 32, 32, 0, 0, 0);
setTileProperties(4, FlxObject.NONE, PlayerInWater);
}
override public function update():void {
super.update();
FlxG.collide(Registry.player, this);
}
private function PlayerInWater(tile:FlxTile, player:Player):void {
Registry.player.inWater = true;
trace("player is in the water");
}
So, I have figured out an answer to this, for all of you who want to know.
I used the overlapsPoint() method to create this:
private function createMap():void {
//load Map
loadMap(new currentMap, currentTiles, 32, 32, 0, 0, 0);
//set water tiles
setTileProperties(4, FlxObject.NONE);
//put all of the water tiles in an array
waterArray = getTileCoords(4);
}
override public function update():void {
super.update();
//collision with map
FlxG.collide(Registry.player, this);
//loops through each water tile
for each(var waterTile:FlxPoint in waterArray) {
//checks if the player is overlapping the water tile
if (Registry.player.overlapsPoint(waterTile)) {
//if so, then the player is in water
Registry.player.inWater = true;
}
}
}
This method being so simple, it kind of makes me feel stupid for getting stuck on it, but if someone finds a more efficient method, please, go ahead and comment, or make a new answer. I always appreciate feedback.

In AS3, how can I check to see if mouse is inside a rectangle?

In AS3, how can I check to see if mouse is inside a rectangle after the stage has been clicked in that area?
for example:
var rec:Rectangle = new Rectangle(50, 200, 50, 200);
And the onclick function would do:
if (mouseX <=rec.left && etc...
I've tried many methods, but have still come up empty. Any ideas?
(What I really wanted to do was make an event listener for the rectangle, but it kept throwing errors so I'm trying to figure out a way to do it with an if statement instead.)
This isn't pertinent that I know this, because I can just do this with flat numbers. I'm trying to learn more about rectangles and points. I figured this would work with a rectangle too, but nope.
You can use Rectangles function contains which accepts 2 arguments x and y.
Like this:
var r:Rectangle = new Rectangle(50,200,50,200);
stage.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
if(r.contains(e.stageX,e.stageY))
{
trace("inside");
}
}

How to check if mouse is on a shpae in Flash

I am building a simple flash game in AS3 and I was wondering if I could use code similar to "hitTestPoint()" except it applies to a shape and not a symbol?
The maze is simply a line shape, so if the ball moves off the shape then the game is terminated. Is this possible?
Thanks,
Peter
Simple enough. Just test if the maze is found at the current location of the ball.
function test():Boolean {
// First we get the absolute coordinates of the ball
var loc:Point = ball.localToGlobal(new Point(0,0));
// Next we collect all the DisplayObjects at that coordinate.
var stack:Array = getObjectsUnderPoint(loc);
var found:Boolean = false;
// Now we cycle through the array looking for our maze
for each (var item in stack) {
if (item.name == "mazeShape") {
found = true;
}
}
return found;
}
If you're really interested in whether the mouse (and not the ball) is off the maze, just replace the first line with this:
var loc:Point = new Point(mouseX, mouseY);
Depending how your game looks, you could also use coordinates for this.
Just tell the game if Player > 100 Y it is off the game it's limits = Restart.
It might won't be the most solid solution but it is definetely a way to solve it as I don't believe there is a function for it, please do correct me if I am wrong.
The AS3 Collision Detection Kit will let you detect hits based on color if separating the maze into smaller symbols is not appropriate.