Copy link to clipboard
Copied
i want any body to test y code written below and test them as i shown in picture i want to pass the grey box and green box perfectly near thei border but it collide .
draw two rectangle and give instance name w1 and w2 then put my code to test it you will under stand my problem
addEventListener(Event.ENTER_FRAME ,moveplayer);
var left,right,up,back:Boolean;
// both rectangles were given instancename as w1 and w2 and drawn on stage manually
stage.addEventListener(KeyboardEvent.KEY_DOWN ,keypress);
stage.addEventListener(KeyboardEvent.KEY_UP ,keyup);
//gray box movement key
function keypress(event:KeyboardEvent ):void
{
if (event.keyCode == 37)
{
left = true;
}
else if (event.keyCode==38)
{
up = true;
}
else if (event.keyCode==39)
{
right = true;
}
else if (event.keyCode==40)
{
back = true;
}
}
function keyup(event:KeyboardEvent ):void
{
if (event.keyCode == 37)
{
left = false;
}
else if (event.keyCode==38)
{
up = false;
}
else if (event.keyCode==39)
{
right = false;
}
else if (event.keyCode==40)
{
back = false;
}
}
//grey box movement
function moveplayer(event:Event ):void
{
//hit test
if (w1.hitTestObject(w2))
{
trace("hit");
//function call to repulse back
repulse();
}
else
{
// function call to move
go();
}
//rotation
if (left)
{
rotateit("left");
}
else if (right)
{
rotateit("right");
}
function rotateit( direc:String)
{
if (direc == "left")
{
w1.rotation -= 1;
}
else if (direc == "right")
{
w1.rotation += 1;
}
}
}
//move function
function go()
{
if (up)
{
var x2:Number = w1.x;
var y2:Number = w1.y;
//trace(x2,y2)
var speed:Number = 5;
w1.x += speed * Math.sin(w1.rotation * 2 * Math.PI / 360);
w1.y -= speed * Math.cos(w1.rotation * 2 * Math.PI / 360);
}
}
//repulsion function
function repulse()
{
var x1:Number = w1.x;
var y1:Number = w1.y;
trace(x1,y1);
var speed:Number = 5;
w1.x += - (speed * Math.sin(w1.rotation * 2 * Math.PI / 360));
w1.y -= - (speed * Math.cos(w1.rotation * 2 * Math.PI / 360));
}
you can use:
package com.kglad{
/*
usage:
import com.kglad.HitF; // execute once
var hitF:HitF = HitF.getInstance(); // singleton HitF. execute once
if(hitF.hit(mc1,mc2)){ // pixel-based hittest
//
}
*/
import flash.display.BitmapData;
import flash.geom.*;
import flash.display.DisplayObject;
import flash.utils.getQualifiedClassName;
import flash.display.Bitmap;
public class HitF{
public static var instance:HitF;
private
...Copy link to clipboard
Copied
use a shape-based hittest with the bitmapdata's hittest.
Copy link to clipboard
Copied
can you give some examples or can you put the line of code in my code
Copy link to clipboard
Copied
you can use:
package com.kglad{
/*
usage:
import com.kglad.HitF; // execute once
var hitF:HitF = HitF.getInstance(); // singleton HitF. execute once
if(hitF.hit(mc1,mc2)){ // pixel-based hittest
//
}
*/
import flash.display.BitmapData;
import flash.geom.*;
import flash.display.DisplayObject;
import flash.utils.getQualifiedClassName;
import flash.display.Bitmap;
public class HitF{
public static var instance:HitF;
private var dataObj:Object;
public static function getInstance():HitF {
if (instance==null) {
instance = new HitF( new SingletonEnforcer() );
}
return instance;
}
public function HitF( pvt:SingletonEnforcer ) {
dataObj = {};
}
public function hit(dobj1:DisplayObject,dobj2:DisplayObject):Boolean{
if(!dobj1 || !dobj2){
return false;
}
if(!dobj1.stage || !dobj2.stage){
return false;
}
if(!dobj1.hitTestObject(dobj2)){
return false;
}
if(!dataObj[dobj1.name]){
dataObj[dobj1.name] = {};
}
if(!dataObj[dobj2.name]){
dataObj[dobj2.name] = {};
}
dataObj[dobj1.name].currentMat = dobj1.transform.concatenatedMatrix;
dataObj[dobj2.name].currentMat = dobj2.transform.concatenatedMatrix;
checkForChangeF(dobj1);
checkForChangeF(dobj2);
//
if(dataObj[dobj1.name]["bmpd"].hitTest(new Point(0,0),255,dataObj[dobj2.name]["bmpd"],new Point(0,0),255)){
return true;
} else {
return false;
}
}
private function checkForChangeF(dobj:DisplayObject):void{
var createBMPDBool:Boolean = false;
if(getQualifiedClassName(dobj).indexOf("MovieClip")>-1){
if(dataObj[dobj.name].previousFrame!=dataObj[dobj.name].currentFrame){
dataObj[dobj.name].previousFrame = dataObj[dobj.name].currentFrame;
createBMPDBool = true;
}
}
if (!dataObj[dobj.name]["bmpd"] || dataObj[dobj.name].currentMat.tx != dataObj[dobj.name].previousMat.tx || dataObj[dobj.name].currentMat.ty != dataObj[dobj.name].previousMat.ty || dataObj[dobj.name].currentMat.a != dataObj[dobj.name].previousMat.a || dataObj[dobj.name].currentMat.b != dataObj[dobj.name].previousMat.b || dataObj[dobj.name].currentMat.c != dataObj[dobj.name].previousMat.c || dataObj[dobj.name].currentMat.d != dataObj[dobj.name].previousMat.d) {
dataObj[dobj.name].previousMat = dataObj[dobj.name].currentMat;
createBMPDBool = true;
}
if(createBMPDBool){
createBmpdF(dobj);
}
}
private function createBmpdF(dobj:DisplayObject):void{
if(dataObj[dobj.name]["bmpd"]){
dataObj[dobj.name]["bmpd"].dispose();
}
dataObj[dobj.name]["bmpd"] = new BitmapData(dobj.stage.stageWidth,dobj.stage.stageHeight,true,0x22000000);
dataObj[dobj.name]["bmpd"].draw(dobj,dataObj[dobj.name].currentMat);
}
public function clearF(dobj:DisplayObject):void{
if(dataObj[dobj.name]){
delete dataObj[dobj.name];
}
}
}
}
internal class SingletonEnforcer{}
Copy link to clipboard
Copied
thanx for help and i ll try this and inform here ...
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now