ActionScript 3.0 TransformGestureEvent and MouseEvent - actionscript-3

I'm trying to use GESTURE_PAN to move a MC and also use MOUSE_CLICK to go to another frame.
Many people are saying GESTURE_PAN and MOUSE_CLICK work together BUT THEY WON'T ON MY iPhone.
I've read many Q&A on the web and of course on stackoverflow.
stop();
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_PAN, onPan);
table.addEventListener(MouseEvent.CLICK, gotoBox);
function onPan(event: TransformGestureEvent): void
{
table.x += event.offsetX;
table.y += event.offsetY;
if (table.x > (table.width / 2))
{
table.x = table.width / 2;
}
if (table.x < stage.stageWidth - (table.width / 2))
{
table.x = stage.stageWidth - (table.width / 2)
}
if (table.y > (table.height / 2))
{
table.y = table.height / 2;
}
if (table.y < stage.stageHeight - (table.height / 2))
{
table.y = stage.stageHeight - (table.height / 2)
}
}
function gotoBox(e: MouseEvent)
{
trace("mouse")
}
The thing is when i test it on Flash Pro CC Simulator everything works good but not on my iDevice.
Help, I'm running out of time!

There is a special settings mapTouchToMouse flag to change MouseEvent dispatching behavior on device, by default it's set to true so you have mouse events in addition to touch events.
But that's mostly done for backward compatibility with desktop/browser apps and has certain performance overhead on mobile devices, so bast practice is to set that flag to false and use some mouse/touch events abstraction like Starling touch system or Gestouch
So check if you changed that mapping to false somewhere, that's the most probable reason why you don't have mouse events.

Related

How to stop a movable object (which moves via the arrow keys) from going off the stage in Adobe Flash Professional CS6 Action Script 3.0

Hey I'm currently creating a game in Adobe Flash Professional CS6. I have a character, with an instance name of "alien".
So far, I've only been able to code my game so that the alien can't go off the top or left sides of the stage. I can't figure out how to code it so that the alien can't go off the bottom or right sides of the stage. The coding I have is as follows:
if((alien.x) < (alien.width/2)){
alien.x += 10;
}
if((alien.y) < (alien.width/2)){
alien.y += 10;
}
Thank you for your time.
Use stage.stageWidth and stage.stageHeight values to determine the size of the stage area. It is not mandatory to use Rectangle, but I like how self-explanatory it is.
import flash.geom.Rectangle;
// new Rectangle(left, top, width, height)
var aBounds:Rectangle = new Rectangle(
alien.width / 2,
alien.height / 2,
stage.stageWidth - alien.width,
stage.stageHeight - alien.height
);
if (alien.y < aBounds.top) alien.y = aBounds.top;
if (alien.x < aBounds.left) alien.x = aBounds.left;
if (alien.x > aBounds.right) alien.x = aBounds.right;
if (alien.y > aBounds.bottom) alien.y = aBounds.bottom;

collision as3 dectect

hi im trying to make a basic game on adobe flash as3 to help learn collision dection, the aim is to make your way through traffic. the player (box_MC) has to make it to the other side and the other objects are in the way (cycle) which have collision detection. i did the collision detection by going into cycle movieclip and making other smaller cycles which if you hit creates collision.
the collision error lies with if the player moves down onto the cycle it doesnt run the collision
p.s if there is a better way of doing the collision how is it done?
hitTestObject() and hitTestPoint() aren't very good when it comes to collision detection, which is somewhat ironic since, of course, those are the first things most people look at when trying to implement collisions. However, I've found that simple math (like, really simple) combined with an equally simple while() loop is the best way to go about it.
What I do is:
// the stage collision box
var mStageRect:Rectangle = new Rectangle(/*stage collision box properties here*/);
// create a Point object that holds the location of the bottom center of the player
var mPlayerBase:Point = new Point(player.x + (player.width / 2), player.y + player.height);
// call this function every frame through your game loop (onEnterFrame)
private function checkCollision(e:Event):void
{
// while the player's bottom center point is inside of the stage...
while (rectContainsPoint(mStageRect, mPlayerBase))
{
// decrement the player's y
player.y--;
// set gravity to 0
player.gravity = 0;
// set isOnGround to true
player.isOnGround = true;
}
}
// checks if a point is currently positioned within the bounds of a rectangle object using ultra simple math
private function rectContainsPoint(rect:Rectangle, point:Point):Boolean
{
return point.x > rect.x && point.x < rect.x + rect.width && point.y > rect.y && point.y < rect.y + rect.height;
}
This is waaaaaaay more efficient than hitTestObject/Point, imo, and has given me no problems.

Multiple Resolution Support in Cocos2d-x v2.2.5 in Portrait mode

I am working on a game in cocos2d-x which is in portrait mode.
Now, for a long time now, I've been working on how to properly achieve multi resolution in cocos2d-x but failed. I followed this great tutorial on forum, but it wasn't enough, I also searched a lot but couldn't find a solution.
I also tried with different-different policies which are available with cocos-x.
I went through all the following links & tutorials
Using these links I could achieve for all ios resolutions but not for all android screens.
http://becomingindiedev.blogspot.in/2014/05/multi-resolution-support-in-ios-with.html
http://discuss.cocos2d-x.org/t/porting-ios-game-to-android-multi-resolution-suppor/5260/5
https://www.youtube.com/watch?v=CH9Ct4R0nBM
https://github.com/SonarSystems/Cocos2d-x-v3-C---Tutorial-4---Multi-Resolution-Support
I even tried with newer version of cocos2d-x, but they also not providing anything which can support both ios and android screens.
I use the following bit of code in my AppDelegate:
void AppDelegate::multiresolutionSupport()
{
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
cocos2d::Size designSize = cocos2d::Size(320, 480);
cocos2d::Size resourceSize = cocos2d::Size(320, 480);
cocos2d::Size screenSize = glview->getFrameSize();
float margin1 = (320 + 640) / 2;
float margin2 = (768 + 1536) / 2;
if (screenSize.width < margin1) {
FileUtils::getInstance()->addSearchResolutionsOrder("SD");
} else if (480 <= screenSize.width && screenSize.width < margin2) {
FileUtils::getInstance()->addSearchResolutionsOrder("HD");
designSize = cocos2d::Size(screenSize.width / 2, screenSize.height / 2);
} else {
FileUtils::getInstance()->addSearchResolutionsOrder("HDR");
designSize = cocos2d::Size(screenSize.width / 4, screenSize.height / 4);
}
resourceSize = screenSize;
director->setContentScaleFactor(resourceSize.width / designSize.width);
glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);
}
Call it before loading any assets and you should have it working properly. I call it right after my glview is created in AppDelegate::applicationDidFinishLaunching().
How it works:
it uses a bunch of magic numbers to determine (roughly) what texture
resolution should we use: SD(#1), HD(#2) or HDR(#4) and then adjusts
design size accordingly, by dividing it on the content scale factor
it adds appropriate search paths to FileUtils it sets design
resolution and content scale factor for the engine
We are the team who made the multi resolution tutorial for the github link, it does support Android but the folders are just named using iOS naming conventions thats all.
Hope this helps.
Regards
Sonar Systems
It's working for me this way for Portrait mode:
Below the header :
typedef struct tagResource
{
cocos2d::CCSize size;
char directory[100];
} Resource;
static Resource smallResource = { cocos2d::CCSizeMake(640, 960),"iPhone" };
static Resource mediumResource = { cocos2d::CCSizeMake(768, 1024),"iPad"};
static Resource largeResource = { cocos2d::CCSizeMake(1536, 2048),"iPadhd" };
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(768, 1024);
in applicationDidFinishLaunching() method :
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
CCSize frameSize = pEGLView->getFrameSize();
std::vector<std::string> searchPaths;
// Set the design resolution
pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionExactFit);
if (frameSize.height <= smallResource.size.height)
{
searchPaths.push_back(mediumResource.directory);
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);
}
else if (frameSize.height <= mediumResource.size.height)
{
searchPaths.push_back(mediumResource.directory);
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);
}
else
{
searchPaths.push_back(largeResource.directory);
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height);
}

Changing the value of a Number Bug AS3

Hey everyone so I am having some trouble here. Been at it for an hour now and can't find a solution.
So I Have a movie clip named _Bunny added to the stage like so:
_Bunny = new mcBunny;
stage.addChild(_Bunny);
_Bunny.x = (stage.stageWidth / 2) - 225;
_Bunny.y = (stage.stageHeight / 2) - 330;
Now what this _Bunny does is move across the stage horizontally from right to left in a loop which i have set up like so in a Enter_Frame event listener:
private function bunnyView():void
{
_Bunny.x += nBunnySpeed;
if (_Bunny.x >=(stage.stageWidth / 2) + 215)
{
_Bunny.gotoAndStop("leftView");
nBunnySpeed--;
}
if (_Bunny.x <=(stage.stageWidth / 2) - 215)
{
_Bunny.gotoAndStop("rightView");
nBunnySpeed++;
}
}
It's speed is the nBunnySpeed which is equal to 5. Now I have another function that I am trying to change the value of the nBunnySpeed to say 20 whenever the nScore is equal to 1 like so:
private function updateDifficulty():void
{
if (nScore >= 1)
{
//Increase Speed
nBunnySpeed = 20;
}
but the bug that in which is produces is the Bunny shooting off to the right side of the screen which is the "+x" no matter what I do this always happens.
Can anyone see what I might be doing wrong? I don't understand why this is happening. Please help!
You need some kind of a flag that indicates that the difficulty has been already updated. Then, when you call updateDifficulty it first checks if there's a real need to update speed now, if there's none, it just returns. If yes, however, then you update your bunny's speed and set that flag so that the next time the function will not alter the bunny's speed.
var diffUpdated:Boolean=false;
private function updateDifficulty():void
{
if (diffUpdated) return; // here
if (nScore >= 1)
{
//Increase Speed
if (nBunnySpeed<0) nBunnySpeed=-20;
else nBunnySpeed = 20; // retain the direction of bunny's movement
}
diffUpdated=true;
}
Now, whenever you want your difficulty to be updated, you do diffUpdated=false; and voila, bunny's speed will be updated by this. For this, however, you will need more than two levels of speed, maybe one for 10 score, one for 50 and one for say 200.
What you do in this function
private function bunnyView():void
{
_Bunny.x += nBunnySpeed;
if (_Bunny.x >=(stage.stageWidth / 2) + 215)
{
_Bunny.gotoAndStop("leftView");
nBunnySpeed--;
}
if (_Bunny.x <=(stage.stageWidth / 2) - 215)
{
_Bunny.gotoAndStop("rightView");
nBunnySpeed++;
}
}
is check whether the _Bunny is offscreen or not. And if so, the nBunnySpeed will be nBunnySpeed - 1. But since BunnySpeed = 20, it will be 20 + 19 + 18 + 17, still going right. If you'd to turn it to BunnySpeed = -BunnySpeed, it will reverse immediately and go back.

how to pinch zoom picture from point of gesture as3

I create an image object and I was able to enlarge .
but images can only be in view of the center of the image .
so how I can enlarge the image of the point of the gesture ??
picture.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
function onZoom(e:TransformGestureEvent){
picture.scaleX *= (e.scaleX+e.scaleY)/2;
picture.scaleY *= (e.scaleX+e.scaleY)/2;
}
Try this...
import flash.events.TransformGestureEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import fl.motion.MatrixTransformer;
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM , onZoom);
function onZoom(event:TransformGestureEvent):void
{
//trace(e.localX);
var locX:Number=event.localX;
var locY:Number=event.localY;
var stX:Number=event.stageX;
var stY:Number=event.stageY;
var prevScaleX:Number=gambar.scaleX;
var prevScaleY:Number=gambar.scaleY;
var mat:Matrix;
var externalPoint=new Point(stX,stY);
var internalPoint=new Point(locX,locY);
gambar.scaleX *= event.scaleX;
gambar.scaleY *= event.scaleY;
if(event.scaleX > 1 && gambar.scaleX > 6){
gambar.scaleX=prevScaleX;
gambar.scaleY=prevScaleY;
}
if(event.scaleY > 1 && gambar.scaleY > 6){
gambar.scaleX=prevScaleX;
gambar.scaleY=prevScaleY;
}
if(event.scaleX < 1.1 && gambar.scaleX < 1){
gambar.scaleX=prevScaleX;
gambar.scaleY=prevScaleY;
}
if(event.scaleY < 1.1 && gambar.scaleY < 1){
gambar.scaleX=prevScaleX;
gambar.scaleY=prevScaleY;
}
mat=gambar.transform.matrix.clone();
MatrixTransformer.matchInternalPointWithExternal(mat,internalPoint,externalPoint);
gambar.transform.matrix=mat;
}
gambar.addEventListener(MouseEvent.MOUSE_DOWN, f_begin);
gambar.addEventListener(MouseEvent.MOUSE_UP, f_end);
function f_begin(e:MouseEvent):void
{
gambar.startDrag();
}
function f_end(e:MouseEvent):void
{
gambar.stopDrag();
}
btw your pic is so cute.. :D
Welcome to StackOverflow. I don't usually work with touch events, as I develop software for desktop, not mobile. However, I do have an idea. I'm not going to venture with code, because without any experience writing touch-related event listeners, I'd probably get something wrong. However, I'm pretty confident that the method will work.
For each event in AS3, there is quite a bit of data about it, as you know. One piece of data generally present on mouse- and touch-related events is location - where on the screen or the object the touch occurred.
I would recommend using this data to adjust the location of the picture on the screen, by using the coordinates of the touch event as the new "center" of the view.
Again, I have no code to offer, because I've never written a touch-related event listener before (I'm always working with mouse and keyboard events.) However, I will venture that the likely source of this information, if touch is anything like the mouse, would be found in the e.x and e.y properties within your onZoom function.
I would also venture to say that you could calculate the new position of your picture with an algorithm something like what follows. I'm assuming the picture's registration point is upper left. [THIS IS NOT CODE]
w=picture width
h=picture height
x=picture x position on stage
y = picture y position on stage
tx=touch event x on stage
ty=touch event y) on stage
x = (w/2) - tx
y = (h/2) - ty
Hope this helps!
EDIT: To access the x, y, w, and h on the picture named "picture", simply use the properties picture.x, picture.y, picture.width, and picture.height as your variables.
One other caveat...the x and y on the EVENT may be on the stage, or on the object itself. You'll want to use trace statements and some common sense to figure this out, and may require some more complex math. I don't want to give you TOO much, though, as this is coursework, like you said.
Have a look at GesTouch, it's a great library for touch action
Gestouch Library
Gestouch Examples