Character hitTest Wall - actionscript-3

The problem with my current platform game project is that the character stops before hitting a wall on the character's left side, and stops too late on the right side of the character.
Here is the script related to the problem:
char.topBumping=false;
char.bottomBumping=false;
char.leftBumping=false;
char.rightBumping=false;
char.speed=0;
char.maxSpeedConstant=10;
char.minSpeedConstant=-10;
char.xVel=0;
char.yVel=0;
stage.addEventListener(Event.ENTER_FRAME,EnterFrame);
function EnterFrame(e:Event){
//local points
top_left_point_local = new Point(char.top_left.x,char.top_left.y);
bottom_left_point_local = new Point(char.bottom_left.x,char.bottom_left.y);
top_right_point_local = new Point(char.top_right.x,char.top_right.y);
bottom_right_point_local = new Point(char.bottom_right.x,char.bottom_right.y);
//global points
top_left_point = new Point(char.localToGlobal(top_left_point_local).x,char.localToGlobal(top_left_point_local).y);
bottom_left_point = new Point(char.localToGlobal(bottom_left_point_local).x,char.localToGlobal(bottom_left_point_local).y);
top_right_point = new Point(char.localToGlobal(top_right_point_local).x,char.localToGlobal(top_right_point_local).y);
bottom_right_point = new Point(char.localToGlobal(bottom_right_point_local).x,char.localToGlobal(bottom_right_point_local).y);
if(ground.hitTestPoint(top_left_point.x,top_left_point.y,true)){
char.leftBumping=true;
}
if(ground.hitTestPoint(bottom_left_point.x,bottom_left_point.y,true)){
char.leftBumping=true;
}
if(!ground.hitTestPoint(top_left_point.x,top_left_point.y,true)&&!ground.hitTestPoint(bottom_left_point.x,bottom_left_point.y,true)){
char.leftBumping=false;
}
if(ground.hitTestPoint(top_right_point.x,top_right_point.y,true)){
char.rightBumping=true;
}
if(ground.hitTestPoint(bottom_right_point.x,bottom_right_point.y,true)){
char.rightBumping=true;
}
if(!ground.hitTestPoint(top_right_point.x,top_right_point.y,true)&&!ground.hitTestPoint(bottom_right_point.x,bottom_right_point.y,true)){
char.rightBumping=false;
}
if(char.rightBumping){
if(char.xVel>0){
char.xVel=0;
char.speed=0;
}
}
if(char.leftBumping){
if(char.xVel<0){
char.xVel=0;
char.speed=0;
}
}
char.x+=char.xVel;
char.y+=char.yVel;
}
Has anyone else encountered this problem? Any help will be much appreciated.
Update:
This is the heart of the problem, for some reason the character hitting the left wall comes out true here even while the character is standing still (left is not being pressed).

Well, after many aggravating hours I finally solved the problem. The orientation of the inside of a movieclip determines its overall position. I never knew that. I always thought it didn't matter how the inside of a movieclip was positioned in relation to the movieclip's center. Lesson learned, always center the inside of movieclips to the mc's stage to simplify things.

Related

how to connect a number variable to dynamic text in actionscript 3.0?

i know this might be simple but i have been searching everywhere for a fix but i just cannot find it!
i want to make something like a health #, so when you press whatever button the dynamic text # will go up or down. on my test project i have two layers, the first with the following code
var hp:Number = 100;
health.text = String hp;
hp being the variable, and health being the dynamic text. then i have the next layer with the button with:
function button(e:MouseEvent):void
{
hp -= 10;
}
without that second chunk of code, the dynamic text will appear, but once that is added it will disappear and the button is function-less.
how do i make this work??? once again sorry if this is a dumb question, i'm just very stumped.
The accepted answer is good, but I wanted to point out that your original code was actually very close to being correct, you just needed parenthesis:
health.text = String(hp);
For most objects String(object) and object.toString() has the same effect, except that object.toString() throws an error if object is null (which could be desirable or undesirable, depending on what you expect it to do).
This is not correct:
health.text = String hp;
use:
health.text = hp.toString();
and:
function button(e:MouseEvent):void
{
hp -= 10;
health.text = hp.toString();
}

CCPhysicsjoint not keeping the 2 bodies intact

I want to add a particle body(say Fire) before another physics body(the arrow).
So, i am using the spring joint to keep them joined with stiffness around 20000, but still its starting the correct way and the particle-body swings away form the arrow. Here's my code:
fire part
CCParticleSystem *explosion= [CCParticleSystem particleWithFile:#"bomb.plist"];
explosion.position = ccpAdd(projectilePos, ccp(16,0));
explosion.physicsBody.mass=1.0;
explosion.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:16.0 andCenter:explosion.anchorPointInPoints];
explosion.physicsBody.type=CCPhysicsBodyTypeDynamic;
explosion.physicsBody.affectedByGravity=FALSE;
explosion.physicsBody.allowsRotation=FALSE; //so that fire flames go upwards only, and not rotate
[physicsWorld addChild:explosion];
explosion.physicsBody.collisionMask=#[];
arrow part
CCSprite *tempSprite;
tempSprite = [CCSprite spriteWithImageNamed:#"arrow3#2x.png"];
[tempSprite setScale:0.05];
[tempSprite setScaleY:0.15];
tempSprite.rotation=rotation;
tempSprite.position=dummyBow.position;
tempSprite.name=[NSString stringWithFormat:#"%#,%d",[lifeOfSprite[1] objectForKey:#"name"],[[lifeOfSprite[1] objectForKey:#"life"] intValue]];
tempSprite.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, tempSprite.contentSize} cornerRadius:0]; // 1
tempSprite.physicsBody.collisionGroup = #"playerGroup";
tempSprite.physicsBody.collisionType = #"projectileCollision";
[physicsWorld addChild:tempSprite z:0];
projectile.physicsBody.mass=100;
now the joint part
[CCPhysicsJoint connectedSpringJointWithBodyA:playerBomb.physicsBody bodyB:projectile.physicsBody anchorA:ccp(1,1) anchorB:ccp(600,0) restLength:0.f stiffness:20000.f damping:60.f];
(debugDraw)
the fire should come at the front, but swings back from front by spring effect
Can anyone tell where's the problem?
Thanks in advance.

Creating a maze in actionscript3

I4d like to create a maze in actionscript 3 but without drawing it because a lot of people are saying that timeline code and drawing things is bad so i'd like to create it with an array. I've searched on google and looked over a lot of tutorials, all doing it differently but not one of them uses classes and all that stuff and I'd really like to do it. I have the idea of how to do it, using an array filled with different numbers of characters if there's a wall, nothing etc... And i know how to draw the block with the graphics things then put a if loop and if the number is 0 put nothing if the number is 1 create the block and place it, but then i'm a bit lost on HOW to make the block appear at the same spot where there is a 1 in the array, I looked at tuts where they did something with rows but I couldn't really understand it clearly.
And also I'm not sure if i have to create a new class for the block, and what do I have to put in this class if i do create it? Do i need to create the block in the class, or outside of it? =/
If someone knows what I mean then all help is welcome.
If you need more details please tell me, sorry if it's confused. =3
It looks like your best bet, your path of least resistance in the long run, may lie in doing a little bit more study and some smaller programs before embarking on this. However if you want a 2D maze made with an Array, you could just do this:
private var m_arrMaze:Array = new Array(40);
private function someFunc():void
{
for (var i:int = 0; i < m_arrMaze.length; i++)
{
m_arrMaze[i] = new Array(50);
}
m_arrMaze[0][0] = 1;
m_arrMaze[0][1] = 1;
.
.
.
m_arrMaze[24][24] = 3;
.
.
.
m_arrMaze[49][49] = 0;
}
This is because you seemed to mention using an array and setting its elements to certain int values to denote what each little spot or room or whatever in the maze is or has. The reason a lot of tutorials may not use a whole lot of classes is because, if this is all you're doing with it, you really don't need too many different classes to denote the stuff in the maze. Just instead of using hard-coded int values, go ahead and put them in constants at the top of your maze class:
private static const EMPTY_SPACE:int = 0;
private static const WALL:int = 1;
.
.
.
private static const PLAYER:int = 3;
private var m_arrMaze:Array = new Array(40);
private function someFunc():void
{
for (var i:int = 0; i < m_arrMaze.length; i++)
{
m_arrMaze[i] = new Array(50);
}
m_arrMaze[0][0] = WALL;
m_arrMaze[0][1] = WALL;
.
.
.
m_arrMaze[24][24] = PLAYER;
.
.
.
m_arrMaze[49][49] = EMPTY_SPACE;
}
If each type of contents within the maze is liable to have a whole different set of nouns, verbs, and adjectives associated with it, instead of just being a different type of marker of where something's at like in the examples above, and if the program is going to do a lot of different things with those contents, that's when you want to use a whole bunch of different classes. Hopefully this will get you started.

actionscript 3 (about ysqu, ydf, and ymo)(making menu)

i am handling with a flash assignment right now.
however, there is trouble from changing as2 to as3
will u know what is the code for as3?
onClipEvent (enterFrame) {
Ysqu = _root.menu._y;
Ydf = Ypa-Ysqu;
Ymo = Ydf/10;
_root.menu._y = Ysqu+Ymo;
updateAfterEvent(enterFrame);
}
thanks so much!!
To know conversions, you must understand them...
addEventListener(Event.ENTER_FRAME, sup);
function sup(e:Evt):void
{
Ysqu = MovieClip(root).menu.y;
Ydf = Ypa-Ysqu;
Ymo = Ydf/10;
MovieClip(root).menu.y = Ysqu+Ymo;
e.updateAfterEvent
}
You will get a warning about updateevent, but it still works.
_y is changed to y. OnClipEvent is changed to addEventListener. _root is changed to MovieClip(root).

erasing a layer where mouse is over it

I got the following question.
i added the following elements to the stage:
homeBg = new HomeBg();
homeMask = new HomeDrawBg();
addChild(homeBg);
addChild(homeMask);
I allready instantiated them in the beginning of the document. But my problem is the following. the homeBg layer is a image, the homeMask layer is the same image but it has a pencil scetch look. What i want is that wherever i move my mouse, the homemask layer should be erased so the bottom layer becomes visible(only where the mask is erased). So how can i tell the mask layer to erase itself if the mouse is over it?
Answer attempt 2
You can use the blendMode property of a display object to achieve this. Here's the code (tested):
// set the eraser width (diameter)
var eraserWidth:int = 20;
//get the HomeMask library item
var homeMask:HomeMask = new HomeDrawBg();
homeMask.blendMode = BlendMode.LAYER;
addChild(homeMask);
// create the eraser shape
var eraser:Shape = new Shape();
eraser.graphics.beginFill(0x000000);
eraser.graphics.drawCircle(0,0,eraserWidth/2);
eraser.blendMode = BlendMode.ERASE;
homeMask.addChild(eraser);
homeMask.addEventListener(MouseEvent.MOUSE_MOVE,mouseOverMask);
function mouseOverMask(evt:MouseEvent):void
{
eraser.x = homeMask.mouseX;
eraser.y = homeMask.mouseY;
}
There are a couple of important bits here.
Firstly, you must set the blendMode of the thing you want to erase to BlendMode.LAYER.
Secondly (and this is what has tricked me before) your eraser, with BlendMode.ERASE, must be a child of the object you're wanting to erase.
Have a look on the following:http://www.piterwilson.com/personal/2008/05/07/bitmapdata-erasing-in-as3-with-custom-brush-shape/