collision detection on rectangle and ball - actionscript-3

i am build ballz game in action script3.
i am facing a problem in corner of rectangle when hit ball it cannot determine ball next position left,or bottom.
this is my code plz help me.
this is the collision function that check direction of ball
`if (isColliding)
{
var brick:Rectangle = hitBrick.getBounds(this);
var bl:int = ba.x - (ball.width / 2);
var br:int = ba.x + (ball.width / 2);
var bt:int = ba.y - (ball.height / 2);
var bb:int = ba.y + (ball.height / 2);
var hl:int = pt.x - (hitBrick.width / 2);
var hr:int = pt.x + (hitBrick.width / 2);
var ht:int = pt.y - (hitBrick.height / 2);
var hb:int = pt.y + (hitBrick.height / 2);
if (hb <= bt)
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - " + "Bottom.");
_totalScore++;
ball._orignalYSpeed = -ball.yspeed;
ball.yspeed = -ball.yspeed + 5;
ball._stopBall = true;
ball._bottomOne = true;
ball.updateCounterInBricks(hitBrick);
return;
}
if ((hl >= bl))
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - Left");
_hitOnWall = true;
ball._orignalXSpeed = -ball.xspeed;
var xSp:Number = ball.xspeed = -ball.xspeed - 5;
ball._left = true;
ball.updateCounterInBricks(hitBrick);
_totalScore++;
return;
}
if(hb<=bb)
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - " + "Bottom.");
_totalScore++;
ball._orignalYSpeed = -ball.yspeed;
ball.yspeed = -ball.yspeed + 5;
ball._stopBall = true;
ball._bottomOne = true;
ball.updateCounterInBricks(hitBrick);
return;
}
if ((hr <= br))
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - Rigth");
_hitOnWall = true;
ball._orignalXSpeed = -ball.xspeed;
var xSp:Number = ball.xspeed = -ball.xspeed + 5;
ball._left = true;
ball._stopBall = true;
ball.updateCounterInBricks(hitBrick);
_totalScore++;
return;
}
if (ht >= bt)
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - " + "Top");
_totalScore++;
ball._orignalYSpeed = -ball.yspeed;
ball.yspeed = -ball.yspeed - 5;
ball._topOne = true;
ball._stopBall = true;
ball.updateCounterInBricks(hitBrick);
return;
}
}`

Related

Character keeps falling when it shouldn't be

I am doing an assessment task for school in as3 and I have made a feature that will let the character fall if it is not standing on top of something but the boolean variable falling keeps being on when it is not supposed to. I have figured out that what causes it to stop for a short time and then fall again past what it is standing on is that the velocity valuable increases past the floor it is on so I reset the velocity value in the code in another spot but that does not solve the issue of the falling boolean being true when it is not supposed too.
I have put a comment at the temporary solution it is in the gameEngine function in the if(falling) statement
// Imports
import flash.events.KeyboardEvent;
import flash.events.Event;
// Constants that can be edited
const grav:Number = 0.05;
const jumpPow:Number = 15;
const walkingDistance = 50;
// Variables
var jumpVel:Number = jumpPow;
var velocity:Number = 0;
var dt:Number = 0;
var movingCount:Number = 0;
var newX:Number = 0;
var newY:Number = 0;
var charCornerHit:Number = 0; // 0 = TL 1 = TR 2 = BR 3 = BL (clockwise)
var jumping:Boolean = false;
var falling:Boolean = false;
var movingRight:Boolean = false;
var movingLeft:Boolean = false;
var hitDetVal:Boolean = false; // false
var floorHit:Boolean = false;
var object1Hit:Boolean = false;
var cCnrTL:Array = new Array(char.x, char.y);
var cCnrTR:Array = new Array(char.x + char.width, char.y);
var cCnrBR:Array = new Array(char.x + char.width, char.y + char.height);
var cCnrBL:Array = new Array(char.x, char.y + char.height);
var charCorners:Array = new Array(cCnrTL, cCnrTR, cCnrBR, cCnrBL); // Clockwise
addEventListener(Event.ENTER_FRAME, gameEngine);
function gameEngine(evt:Event):void{
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCharacter);
charCorners[0][0] = char.x;
charCorners[0][1] = char.y;
charCorners[1][0] = char.x + char.width;
charCorners[1][1] = char.y;
charCorners[2][0] = char.x + char.width;
charCorners[2][1] = char.y + char.height;
charCorners[3][0] = char.x;
charCorners[3][1] = char.y + char.height;
//trace(char.y);
// Check if char is standing on something
// Supposed to only check when character is not falling
if (falling == false) {
//trace(char.y + char.height + 1)
hitDetection(0, char.y + char.height + 1)
if (hitDetVal == false) {
falling = true;
hitDetVal = false;
trace("not standing");
}
else {trace("standing on something");}
}
// Move char loop
if (movingRight){
if (movingCount == walkingDistance) {
movingCount = 0
movingRight = false;
} else {
char.x+=1
movingCount += 1;
}
} else if (movingLeft) {
if (movingCount == walkingDistance) {
movingCount = 0
movingLeft = false;
} else {
char.x-=1
movingCount += 1;
}
}
//trace(velocity)
if (falling) {
dt += 1
velocity = velocity + grav*dt
hitDetection(0, velocity)
if (hitDetVal) {
falling = false;
hitDetVal = false;
// TEMPORARY SOLUTION - Stopped character from falling past the floor but still ran falling condition
//velocity = 0;
//dt = 0;
if (floorHit) {
if (char.y < floor.y){
char.y = floor.y - char.height;
floorHit = false;
trace("Char Stopped falling")
}
} else if (object1Hit) {
if (char.y - char.height < object1.y){
char.y = object1.y - char.height;
object1Hit = false;
trace("Char Stopped falling")
}
}
} else {
char.y += velocity;
}
} else {
if (jumping) {
}
velocity = 0;
dt = 0;
}
}
function moveCharacter(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.LEFT){
if(movingRight){
movingRight = false;
trace("STOPPED")
} else {
movingCount = 0
movingLeft = true;
trace("LEFT");
}
}
if(event.keyCode == Keyboard.RIGHT){
if(movingLeft){
movingLeft = false;
trace("STOPPED")
} else {
movingCount = 0
movingRight = true;
trace("RIGHT");
}
}
if(event.keyCode == Keyboard.UP){
jumping = true;
trace("UP");
}
}
function hitDetection(distX:Number, distY:Number) {
for (var i:uint = 0; i < charCorners.length; i++) {
newX = charCorners[i][0] + distX;
newY = charCorners[i][1] + distY;
if (floor.hitTestPoint(newX, newY, true)){
hitDetVal = true;
floorHit = true;
charCornerHit = i;
break;
} else if (object1.hitTestPoint(newX, newY, true)){
hitDetVal = true;
object1Hit = true;
charCornerHit = i;
break;
}
}
}```
It was a simple logic error occurring here
hitDetection(0, char.y + char.height + 1)
I had made adjustments for the location by adding char.y to the value when my hitDetection function already made an adjustment to each corner of my character. Only took ~3 hours to figure out

Get points in selected are in the teechart

Trying to get series values in the selected area after mouseup.
Need to calculate width and height of the selected are in the chart with respect to axis.
[`https://jsbin.com/wapovohiwe/edit?html,output`][1]
I modified your example to draw the rectangle and to output the coordinates of the
Chart1 = new Tee.Chart("canvas");
Chart1.legend.visible = false;
Chart1.panel.format.gradient.visible = false;
Chart1.panel.format.fill = "white";
Chart1.walls.back.format.fill = "white";
Chart1.addSeries(new Tee.Line([1, 3, 0, 2, 7, 5, 6]));
Chart1.zoom.enabled = false;
var myFormat = new Tee.Format(Chart1);
myFormat.transparency = 0.9;
var rect = {},
drag = false;
Chart1.mousedown = function(e) {
rect.startX = e.x - canvas.offsetLeft - canvas.parentElement.offsetLeft - canvas.parentElement.parentElement.offsetLeft;
rect.startY = e.y - canvas.offsetTop - canvas.parentElement.offsetTop - canvas.parentElement.parentElement.offsetTop;
drag = true;
}
Chart1.mouseup = function(p) {
var i, tmpP = {};
points = [];
var s = Chart1.series.items[0];
for (i = 0; i < s.count(); i++) {
s.calc(i, tmpP);
if ((tmpP.x >= rect.startX) && (tmpP.x <= rect.startX + rect.w) && (tmpP.y >= rect.startY) && (tmpP.y <= rect.startY + rect.h))
console.log("index: " + i + ", value: " + s.data.values[i]);
}
drag = false;
}
Chart1.mousemove = function(e) {
if (drag) {
rect.w = e.x - rect.startX;
rect.h = e.y - rect.startY;
drawChart();
}
}
function drawChart() {
Chart1.draw();
myFormat.rectangle(rect.startX, rect.startY, rect.w, rect.h);
}
Chart1.draw();
<script src="https://www.steema.com/files/public/teechart/html5/latest/src/teechart.js"></script>
<canvas id="canvas" height="400" width="700"></canvas>

Why my app is different when i publish it and run it on android device?

I build a platform game using adobe cs5.5 and written with as3.
When i test my app on pc the game is running perfect but when i
publish it and run it to my android device i have some gravity issues.
Sometimes when i land on ground all the stage start to tremble.
The strange is, in the same level in the same ground sometimes the stage start
tremble and sometimes not.
Is that a problem in my code or something else?
I use 800w x 480h stage.
i publish using GPU mode.
All my ground inside i draw it with shape.
var xvel = 0;
var yvel = 0;
var GRA = 1;
var FRICT = 0.85;
var downarr = new Array(-12,0,12);
var vertarr = new Array(-20,-30);
var uparr = new Array(-3,0,3);
this.addEventListener(Event.ENTER_FRAME,FramesEvents);
function windowcamera()
{
var newx = - savvito.x + 350;
var distx = newx - bg.x;
bg.x += distx;
var newy = - savvito.y + 370;
var disty = newy - bg.y;
bg.y += disty;
}
function phys()
{
savvito.x += xvel;
savvito.y += yvel;
xvel *= FRICT;
yvel += GRA;
if (yvel > 8)
{
yvel = 8;
}
if (yvel > 2)
{
onground = false;
}
savvito.virtx = savvito.x + bg.x;
savvito.virty = savvito.y + bg.y;
downPush(savvito);
leftPush(savvito);
rightPush(savvito);
upPushCloud(savvito);
upPush(savvito);
}
function runJump()
{
onground = false;
yvel = - 12;
}
function downPush(d)
{
d.virtx = d.x + bg.x;
d.virty = d.y + bg.y;
for (var i = 0; i < uparr.length; i++)
{
var num = uparr[i];
var tx = d.virtx + num;
var ty = d.virty - 40;
while (bg.ground.hitTestPoint(tx , ty , true))
{
d.virty++;
ty++;
d.y++;
yvel = 1;
}
for (var ie=0; ie<boxingArr.length; ie++)
{
var e = boxingArr[ie];
if (e.liftthisbox == 0)
{
while (e.hitTestPoint(tx , ty , true))
{
e.y--;
e.yvel = 0;
}
}
}
}
}
function rightPush(d)
{
d.virtx = d.x + bg.x + xvel;
d.virty = d.y + bg.y;
for (var i = 0; i < vertarr.length; i++)
{
var num = vertarr[i];
var tx = d.virtx + 15;
var ty = d.virty + num;
while (bg.ground.hitTestPoint(tx , ty , true))
{
d.virtx--;
tx--;
d.x--;
xvel = 0;
}
for (var ie=0; ie<boxingArr.length; ie++)
{
var e = boxingArr[ie];
if (e.liftthisbox == 0)
{
while (e.hitTestPoint(tx , ty , true))
{
d.virtx--;
tx--;
d.x--;
xvel = 0;
}
}
}
}
}
function leftPush(d)
{
d.virtx = d.x + bg.x + xvel;
d.virty = d.y + bg.y;
for (var i = 0; i < vertarr.length; i++)
{
var num = vertarr[i];
var tx = d.virtx - 15;
var ty = d.virty + num;
while (bg.ground.hitTestPoint(tx , ty , true))
{
d.virtx++;
tx++;
d.x++;
xvel = 0;
}
for (var ie=0; ie<boxingArr.length; ie++)
{
var e = boxingArr[ie];
if (e.liftthisbox == 0)
{
while (e.hitTestPoint(tx , ty , true))
{
d.virtx++;
tx++;
d.x++;
xvel = 0;
}
}
}
}
}
function upPushCloud(d)
{
if (yvel > 0)
{
d.virtx = d.x + bg.x;
d.virty = d.y + bg.y;
for (var i = 0; i < downarr.length; i++)
{
var num = downarr[i];
var tx = d.virtx + num;
var ty = d.virty;
while (bg.cloudbg.hitTestPoint(tx , ty , true))
{
onground = true;
jdown = false;
d.virty--;
ty--;
d.y--;
yvel = 0;
}
}
}
}
function upPush(d)
{
d.virtx = d.x + bg.x;
d.virty = d.y + bg.y;
for (var i = 0; i < downarr.length; i++)
{
var num = downarr[i];
var tx = d.virtx + num;
var ty = d.virty;
while (bg.ground.hitTestPoint(tx , ty , true))
{
onground = true;
jdown = false;
d.virty--;
ty--;
d.y--;
yvel = 0;
}
for (var ie=0; ie<boxingArr.length; ie++)
{
var e = boxingArr[ie];
while (e.hitTestPoint(tx , ty , true))
{
onground = true;
jdown = false;
d.virty--;
ty--;
d.y--;
yvel = 0;
}
}
}
}
function FramesEvents(e:Event)
{
phys();
controls();
windowcamera();
}

Lerping 3d rotation (away3d)

I'm creating a 3d game using away3d and awayphysics.
I've created a rotation formula that will rotate my model with a "smooth factor".
private var chRotation:Number = 0;
public override function update(delta:uint):void
{
if(target){
var smooth:Number = 0.95;
var tp:Vector3D = target.ghostObject.position;
var cp:Vector3D = entity.ghostObject.position;
var targetAngle:Number = -((180 / Math.PI) * Math.atan2(cp.z - tp.z, cp.x - tp.x));
if(oldTgRotation - targetAngle != 0){
if((oldTgRotation - targetAngle) > 300){
chRotation = -180;
}else if((oldTgRotation - targetAngle) < -300){
chRotation = 180;
}
}
chRotation += (targetAngle + (chRotation - targetAngle) * (smooth - (delta / 800))) - chRotation;
entity.ghostObject.rotation = new Vector3D(0, chRotation, 0);
oldTgRotation = targetAngle;
}
}
this works partly, it works until the mesh rotates from -180 to 180 cus the code will then rotate the mesh backwards, so: -180 -90 0 90 180
It should go from -180 to 180 forward. but how?
Edit: I've added kind of a solution but this still isn't perfect:
if(oldTgRotation - targetAngle != 0){
if((oldTgRotation - targetAngle) > 300){
chRotation = -180;
}else if((oldTgRotation - targetAngle) < -300){
chRotation = 180;
}
}
You should use the modulus operator instead of the if.
curRotation = curRotation % 360;
Ok, so to fix this problem I've added a boolean switch:
private var chRotation:Number = 0;
private var switchf:Boolean = false;
private var switchb:Boolean = false;
public override function update(delta:uint):void
{
if(target){
var smooth:Number = 0.95;
var tp:Vector3D = target.ghostObject.position;
var cp:Vector3D = entity.ghostObject.position;
var targetAngle:Number = -((180 / Math.PI) * Math.atan2(cp.z - tp.z, cp.x - tp.x));
if(oldTgRotation - targetAngle != 0){
if((oldTgRotation - targetAngle) > 300){
switchf = true;
}else if((oldTgRotation - targetAngle) < -300){
switchb = true;
}
}
if(switchf){
if(chRotation >= 177){
switchf = false;
chRotation = -180;
}else{
targetAngle = 190;
}
}
if(switchb){
if(chRotation <= -177){
switchb = false;
chRotation = 180;
}else{
targetAngle = -190;
}
}
chRotation += (targetAngle + (chRotation - targetAngle) * (smooth - (delta / 800))) - chRotation;
entity.ghostObject.rotation = new Vector3D(0, chRotation, 0);
oldTgRotation = targetAngle;
}
}

Problems with Wordwrap in Flashpunk

For some reason, the last line of my wordwrap does not display
public class ShopButton extends Entity
{
private var callback:Function = null;
public var buttonText:String = "";
public var textImage:Text;
public var picture:BitmapData = new BitmapData(100, 100, true, 1);
public var boxImage:Stamp;
public var displayImage:Graphiclist;
public var displayImage2:Graphiclist;
public var tooltipPicture:BitmapData = new BitmapData(150, 130, true, 1);
public var tooltipBox:Stamp;
public var tooltipText:Array = [];
public var tooltipString:Array = [];
public var buttonWidth:int = 100;
public var buttonHeight:int = 100;
private var clicked:Boolean = false;
private var shiftClicked:Boolean = false;
private var hovered:Boolean = false;
private var fontSize:int = 16;
public var label:String = "";
public function ShopButton(x:int = 0, y:int = 0, label:String = "", callback:Function = null)
{
this.callback = callback;
this.label = label;
buttonText = label;
textImage = new Text(buttonText, 5, 5, { width:buttonWidth-10, wordWrap:true, align:"center", size:fontSize, font:"Abscissa" } );
Draw.setTarget(picture);
Draw.rectPlus(0, 0, buttonWidth, buttonHeight, 0x000000, 1, true, 2, 5);
Draw.rectPlus(0, 0, buttonWidth, buttonHeight, 0xFFFF00, 1, false, 5, 5);
boxImage = new Stamp(Assets.SHOPBUTTON);
textImage.y = (boxImage.height / 2) - (textImage.height / 2);
displayImage = new Graphiclist(boxImage, textImage);
super (x, y, displayImage);
setHitboxTo(boxImage);
Draw.setTarget(tooltipPicture);
Draw.rectPlus(0, 0, 150, 130, 0x000000, 1, true, 1, 0);
Draw.rectPlus(0, 0, 150, 130, 0xFFFF00, 1, false, 5, 0);
tooltipBox = new Stamp(Assets.SHOPBUTTONBORDER);
updateTooltip();
}
public function updateTooltip():void
{
var minDamage:int = 0;
var maxDamage:int = 0;
var damageResist:int = 0;
switch(label)
{
case "Purchase Ammo":
tooltipString[0] = "";
tooltipString[1] = "Purchase more";
tooltipString[2] = "shurikens";
tooltipString[3] = "";
tooltipString[4] = "Shift + Click";
tooltipString[5] = "To buy max";
break;
case "Upgrade Melee Weapon":
minDamage = Globals.meleeDamage[0][Globals.meleeLevel];
maxDamage = Globals.meleeDamage[1][Globals.meleeLevel];
minDamage = minDamage * (1+(Globals.playerMeleeDamage / 100));
maxDamage = maxDamage * (1 + (Globals.playerMeleeDamage / 100));
tooltipString[0] = "Current Level";
tooltipString[1] = "Damage: "
tooltipString[2] = "" + minDamage + " - " + maxDamage;
tooltipString[3] = "Next Level";
if (Globals.meleeLevel < Globals.meleeMaxLevel)
{
minDamage = Globals.meleeDamage[0][Globals.meleeLevel + 1];
maxDamage = Globals.meleeDamage[1][Globals.meleeLevel + 1];
minDamage = minDamage * (1+(Globals.playerMeleeDamage / 100));
maxDamage = maxDamage * (1 + (Globals.playerMeleeDamage / 100));
tooltipString[4] = "Damage: "
tooltipString[5] = "" + minDamage + " - " + maxDamage;
}
else
{
tooltipString[4] = "";
tooltipString[5] = "Max Level";
}
break;
case "Upgrade Ranged Weapon":
minDamage = Globals.rangedDamage[0][Globals.rangedLevel];
maxDamage = Globals.rangedDamage[1][Globals.rangedLevel];
minDamage = minDamage * (1+(Globals.playerRangeDamage / 100));
maxDamage = maxDamage * (1 + (Globals.playerRangeDamage / 100));
tooltipString[0] = "Current Level";
tooltipString[1] = "Damage: "
tooltipString[2] = "" + minDamage + " - " + maxDamage;
tooltipString[3] = "Next Level";
if (Globals.rangedLevel < Globals.rangedMaxLevel)
{
minDamage = Globals.rangedDamage[0][Globals.rangedLevel + 1];
maxDamage = Globals.rangedDamage[1][Globals.rangedLevel + 1];
minDamage = minDamage * (1+(Globals.playerRangeDamage / 100));
maxDamage = maxDamage * (1 + (Globals.playerRangeDamage / 100));
tooltipString[4] = "Damage: "
tooltipString[5] = "" + minDamage + " - " + maxDamage;
}
else
{
tooltipString[4] = "";
tooltipString[5] = "Max Level";
}
break;
case "Upgrade Armor":
damageResist = Globals.armorDefense[Globals.armorLevel];
damageResist = damageResist * (1 + (Globals.playerDefense / 100));
tooltipString[0] = "Current Level";
tooltipString[1] = "Damage Resist: "
tooltipString[2] = "" + damageResist;
tooltipString[3] = "Next Level";
if (Globals.armorLevel < Globals.armorMaxLevel)
{
damageResist = Globals.armorDefense[Globals.armorLevel + 1];
damageResist = damageResist * ( 1 + (Globals.playerDefense / 100));
tooltipString[4] = "Damage Resist: "
tooltipString[5] = "" + damageResist;
}
else
{
tooltipString[4] = "";
tooltipString[5] = "Max Level";
}
break;
}
tooltipText[0] = new Text(tooltipString[0], 0, 10, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[0].x = -25;
tooltipText[0].y -= 20;
tooltipText[1] = new Text(tooltipString[1], 0, 30, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[1].x = -25;
tooltipText[1].y -= 20;
tooltipText[2] = new Text(tooltipString[2], 0, 45, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[2].x = -25;
tooltipText[2].y -= 20;
tooltipText[3] = new Text(tooltipString[3], 0, 65, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[3].x = -25;
tooltipText[3].y -= 20;
tooltipText[4] = new Text(tooltipString[4], 0, 85, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[4].x = -25;
tooltipText[4].y -= 20;
tooltipText[5] = new Text(tooltipString[5], 0, 100, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[5].x = -25;
tooltipText[5].y -= 20;
tooltipBox.x = -25;
tooltipBox.y = -20;
displayImage2 = new Graphiclist(tooltipBox, tooltipText[0], tooltipText[1], tooltipText[2], tooltipText[3], tooltipText[4], tooltipText[5]);
}
public function click(action:String = ""):void
{
//trace(action);
if (action == "Shift" && label == "Purchase Ammo" && callback != null) callback("Shift");
else if (callback != null) callback();
}
override public function update():void
{
super.update();
if (collidePoint(x, y, world.mouseX, world.mouseY))
{
if (Input.mousePressed && !Input.check(Key.SHIFT))
{
shiftClicked = false;
clicked = true;
}
if (Input.mousePressed && Input.check(Key.SHIFT))
{
//trace ("Shifty");
clicked = false;
shiftClicked = true;
}
if (Input.mouseReleased && clicked) click("");
if (Input.mouseReleased && shiftClicked) click("Shift");
hovered = true;
}
else hovered = false;
if (Input.mouseReleased) clicked = false;
if (hovered)
{
updateTooltip();
graphic = displayImage2;
}
else graphic = displayImage;
}
}
When I run it, it displays all but the last line of text (I guess I need a rep of 10 to post a screenshot). For example, the first button displays "Purchase" instead of "Purchase Ammo".
Has anyone ever run into this issue before, and if so how do you get around it?
I've ran into some problems with word wrap. It seems that for Text class the word wrap works correctly when you specify its width and height and then set the word wrap to true and important - resizable to false.
I was just having the exact same problem, try setting the height attribute, that solved for me.