Get dimensions of stage - flash-cs6

I want the full dimensions of my "stage" or document or whatever it's called. I just started with flash and I've been looking all over with no success.
The closest I came was "stage.height", but it does not do what I wanted. I realized "stage.height" returns 2x size of my object. What I'm trying to achieve is to make sure the object cannot go outside the screen. I use their example movement actionscript code. I attempted to modify it to fit my needs, this is the edited code (where stage.height not working as expected nor width)...
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
Ball.y = Math.max(0, Ball.y - 5);
}
if (downPressed)
{
Ball.y = Math.min(Ball.y + 5, stage.height);
}
if (leftPressed)
{
Ball.x = Math.max(0, Ball.x - 5);
}
if (rightPressed)
{
Ball.x = Math.min(Ball.x + 5, stage.width);
}
}

You should use "stage.stageWidth" for width of stage or "stage.stageHeight" for height of stage.
Remember that if you use nested movie clips into your flash project, and you want to control some objects in them; it's better x and y coordinates of your movie clips that contain your objects set in zero values; if x and y coordinates is set to another value, you must find some calculations for achieve to what you want to do.
If you want to know about differences between stage.stageWidth and stage.width or stage.stageHeight and stage.height, please check following links:
Difference between calling stage.width and stage.stageWidth?
Difference between stage.width and stage.stageWidth
For better understanding, I describe it with an example.
Imagine that we have a stage with 1024px width and 768px height. We create a movie clip symbol onto the stage in 30px x and 70px y coordinates. Now we add something in that movie clip and set x and y coordinates to zero value. If we return to root timeline (main stage), object of movie clip is set in 30px x and 70px y coordinates not in 0. If we want to set x and y coordinates of the object to x and y coordinates of the stage, we must set -30px for x and -70px for y. in this situation, we have to do some calculations for correct control on our object in the movie clip for what we want to do.
I'm created an example for you. Please check it:
Objects Movement
If you want that your objects remain on the stage when stage resizes, you must change "Scale Mode" of stage to No Scale and "Align" of stage to "Top Left" as follows:
import flash.display.StageScaleMode;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
and then, you must add "Resize" event to your stage.
import flash.events.Event;
stage.addEventListener(Event.RESIZE, stage_Resize);
function stage_Resize(event:Event):void
{
enter your codes here
}
With adding Resize event, when stage resizes by end-users, your codes in the Resize event will be fired.
Please check following example:
4Shared -> Resize Example
SendSpace -> Resize Example

Related

Loop to change block position

I have a Matlab script that creates a Model Block for each element i found in a text file.
The problem is that all Models are created on each other in the window. So i'm trying to make a loop like:
for each element in text file
I add a Model block
I place right to the previous one
end
So it can look like this:
As you can see on the left, all models are on each other and I would like to place them like the one on the right.
I tried this:
m = mdlrefCountBlocks(diagrammeName)+500;
add_block('simulink/Ports & Subsystems/Model',[diagrammeName '/' component_NameValue]);
set_param(sprintf('%s/%s',diagrammeName,component_NameValue), 'ModelFile',component_NameValue);
size_blk = get_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position');
X = size_blk(1,1);
Y = size_blk(1,2);
Width = size_blk(1,3);
Height = size_blk(1,4);
set_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position',[X+m Y X+Width Y+Height]);
Inside the loop but it returns an error Invalid definition of rectangle. Width and height should be positive.
Thanks for helping!
The position property of a block does actually not contain its width and height, but the positions of the corners on the canvas (see Common Block Properties):
vector of coordinates, in pixels: [left top right bottom]
The origin is the upper-left corner of the Simulink Editor canvas before any canvas resizing. Supported coordinates are between -1073740824 and 1073740823, inclusive. Positive values are to the right of and down from the origin. Negative values are to the left of and up from the origin.
So change your code to e.g.:
size_blk = get_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position');
set_param(sprintf('%s/%s',diagrammeName,component_NameValue),'Position', size_blk + [m 0 0 0]);

How to reset unity accelerometer on Z?

someone can tell me how can I reset my accelerometer.
I have a 3D environment, and my ball bounce left-right correctly, my Y is ok, I don't need it, but my z is not ok, I use my device in landscape mode.
Pratically when my devide is 90° (landscape mode) with the floor is in neutral z position.
I need to have neutral position when user start game, I see other answer on unity blog with they don't work for me.
This is my code :
public float posZ;
// Use this for initialization
void Start () {
Salvataggio.control.Load ();
posZ = Input.acceleration.z;
}
void Update () {
trovaPallina = GameObject.FindGameObjectWithTag ("Pallina");
if (trovaPallina) {
trovaPallina.transform.position += new Vector3(Input.acceleration.x / 10, 0, (Input.acceleration.z - posZ) / 10) ;
}
}
Why not create some sort of calibration for it? So, you could create a calibration menu in which you click a button once you are satisfied with the zero position of the device. Store the x, y & z values as your zero values. That way, you can use the current values against your zero values to determine the offset in each direction.

Swift: comparing 2 object's positions in if statement not working?

Alright, so I'm trying to do something simple here and I think I've overcomplicated it- I need an if statement saying if this object goes off the screen (into negative y coordinates), have something happen. I can't get it.
I've tried a number of things, including if statements that compare to numbers like this, having it be equal and then trying greater/less than:
if block1.position.y == -50 {
savior.hidden = true
}
I've tried having the object be less than the size of the self.size.height :
if block1.position.y < self.size.height {
savior.hidden = true
}
And I've tried placing an object at the point off the screen and having an if statement comparing the 2 object's y positions:
if block1.position.y == ptBlock1.position.y {
savior.hidden = true
}
And nothing's working. Block1, the object I'm working with, is being sent to the specific point in an SKAction, so I know that it's getting there:
var moveDownLeft = SKAction.moveTo(CGPointMake(self.size.width * 0.35,-50), duration:5.5)
block1.runAction(moveDownLeft)
Why won't the if statement work?
EDIT:
I have tried this, and even when block1 visibly has a y position lower than ptBlock1, nothing happens:
if block1.position.y < ptBlock1.position.y {
savior.hidden = true
}
else if block1.position.y > ptBlock1.position.y {
savior.hidden = false
}
this object goes off the screen (into negative y coordinates)
You're making a false assumption there. Off the screen is not necessarily negative y coordinates.
The position of an SKNode is not measured with respect to screen; it is measured with respect to its superview, which is the SKScene. And the SKScene is much bigger than the screen! You need to convert from those coordinates to screen coordinates if you want to know what's happening relative to the screen.
(Just to give an example, if you make a new SpriteKit project from the template and log on touchesBegan to show the tap position, you will discover that a tap in the top left corner is at about {303,758}. So in that coordinate system an object is off the screen to the top if its y is greater than about 760. Contrast that with screen coordinates, where you are off the screen to the top if you are less than 0. These are very different coordinate systems!)

(Unity3D) Paint with soft brush (logic)

During the last few days i was coding a painting behavior for a game am working on, and am currently in a very advanced phase, i can say that i have 90% of the work done and working perfectly, now what i need to do is being able to draw with a "soft brush" cause for now it's like am painting with "pixel style" and that was totally expected cause that's what i wrote,
my current goal consist of using this solution :
import a brush texture, this image
create an array that contain all The alpha values of that texture
When drawing use the array elements in order to define the new pixels alpha
And this is my code to do that (it's not very long, there is too much comments)
//The main painting method
//theObject = the object to be painted
//tmpTexture = the object current texture
//targetTexture = the new texture
void paint (GameObject theObject, Texture2D tmpTexture, Texture2D targetTexture)
{
//x and y are 2 floats from another class
//they store the coordinates of the pixel
//that get hit by the RayCast
int x = (int)(coordinates.pixelPos.x);
int y = (int)(coordinates.pixelPos.y);
//iterate through a block of pixels that goes fro
//Y and X and go #brushHeight Pixels up
// and #brushWeight Pixels right
for (int tmpY = y; tmpY<y+brushHeight; tmpY++) {
for (int tmpX = x; tmpX<x+brushWidth; tmpX++) {
//check if the current pixel is different from the target pixel
if (tmpTexture.GetPixel (tmpX, tmpY) != targetTexture.GetPixel (tmpX, tmpY)) {
//create a temporary color from the target pixel at the given coordinates
Color tmpCol = targetTexture.GetPixel (tmpX, tmpY);
//change the alpha of that pixel based on the brush alpha
//myBrushAlpha is a 2 Dimensional array that contain
//the different Alpha values of the brush
//the substractions are to keep the index in range
if (myBrushAlpha [tmpY - y, tmpX - x].a > 0) {
tmpCol.a = myBrushAlpha [tmpY - y, tmpX - x].a;
}
//set the new pixel to the current texture
tmpTexture.SetPixel (tmpX, tmpY, tmpCol);
}
}
}
//Apply
tmpTexture.Apply ();
//change the object main texture
theObject.renderer.material.mainTexture = tmpTexture;
}
Now the fun (and bad) part is the code did exactly what i asked for, but there is something that i didn't think of and i couldn't solve after spend the whole night trying,
the thing is that by asking to draw anytime with the brush alpha i found myself create a very weird effect which is decreasing the alpha value of an "old" pixel, so i tried to fix that by adding an if statement that check if the current alpha of the pixel is less than the equivalent brush alpha pixel, if it is, then augment the alpha to be equal to the brush, and if the pixel alpha is bigger, then keep adding the brush alpha value to it in order to have that "soft brushing" effect, and in code it become this :
if (myBrushAlpha [tmpY - y, tmpX - x].a > tmpCol.a) {
tmpCol.a = myBrushAlpha [tmpY - y, tmpX - x].a;
} else {
tmpCol.a += myBrushAlpha [tmpY - y, tmpX - x].a;
}
But after i've done that, i got the "pixelized brush" effect back, am not sure but i think maybe it's because am making these conditions inside a for loop so everything is executed before the end of the current frame so i don't see the effect, could it be that ?
Am really lost here and hope that you can put me in the right direction,
Thank you very much and have a great day

Copying a portion of an IplImage into another Iplimage (that is of same size is the source)

I have a set of mask images that I need to use everytime I recognise a previously-known scene on my camera. All the mask images are in IplImage format. There will be instances where, for example, the camera has panned to a slightly different but nearby location. this means that if I do a template matching somewhere in the middle of the current scene, I will be able to recognise the scene with some amount of shift of the template in this scene. All I need to do is use those shifts to adjust the mask image ROIs so that they can be overlayed appropriately based on the template-matching. I know that there are functions such as:
cvSetImageROI(Iplimage* img, CvRect roi)
cvResetImageROI(IplImage* img);
Which I can use to set crop/uncrop my image. However, it didn't work for me quit the way I expected. I would really appreciate if someone could suggest an alternative or what I am doing wrong, or even what I haven't thought of!
**I must also point out that I need to keep the image size same at all times. The only thing that will be different is the actual area of interest in the image. I can probably use the zero/one padding to cover the unused areas.
I believe a solution without making too many copies of the original image would be:
// Make a new IplImage
IplImage* img_src_cpy = cvCreateImage(cvGetSize(img_src), img_src->depth, img_src->nChannels);
// Crop Original Image without changing the ROI
for(int rows = roi.y; rows < roi.height; rows++) {
for(int cols = roi.x; rows < roi.width; cols++) {
img_src_cpy->imageData[(rows-roi.y)*img_src_cpy->widthStep + (cols-roi.x)] = img_src[rows*img_src + cols];
}
{
//Now copy everything to the original image OR simply return the new image if calling from a function
cvCopy(img_src_cpy, img_src); // OR return img_src_cpy;
I tried the code out on itself and it is also fast enough for me (executes in about 1 ms for 332 x 332 Greyscale image)