Using CGRectIntersectsRect with IBOutletCollection of an array of uiviews - arrays

Hello I have 3 views as part of an IBOutletCollection. They are in the array called myArrayOfViews. I'd like to be able to use CGRectIntersectsRect to determine when any of these 3 views overlap but so far no luck. I thought I could loop through the array twice and then run CGRectIntersectsRect but no luck. What am I missing. Thanks in advance!
for (UIView *view1 in self.myArrayOfViews) {
NSLog(#"view1 is %#",view1);
for (UIView *view2 in self.myArrayOfViews) {
NSLog(#"view2 is %#",view2);
if( CGRectIntersectsRect(view1.frame, view2.frame)) {
NSLog(#"overlap!");
}
}
}

You figured out what the problem was. Here's how to add the check that the two views are not the same:
for (UIView *view1 in self.myArrayOfViews) {
for (UIView *view2 in self.myArrayOfViews) {
if (view1 != view2 && CGRectIntersectsRect(view1.frame, view2.frame)) {
NSLog(#"overlap!");
}
}
}

Related

Refactor checking that an item exist in an array

In my code, I have to check an array contains a specific item or not.
Here is my code
If error.errors[0].peoples = .noteEntered {
Print("something")
}
To learn better, can I ask you to show me how to do it in the better way, I think it's not good.
Many thanks
You can try
if error.errors.contains(where:{$0.peoples == .noteEntered}) {
print("something")
}
If you need the item do
if let item = error.errors.first(where:{$0.peoples == .noteEntered}) {
print(item)
}
if error.errors[0].peoples.contains(.noteEntered) {
print("something")
}

Checking shot collisions and removing shot and target in AS3

Last year was my first successful year in teaching my students to create catch and avoid games in Flash with AS3. This year is getting better. Each year I come here for help at least once.
I'm trying to add shooting into the possibilities for their second game project. I can make the shot happen from the ship, gun, whatever, and make it move, and get rid of it when it is off screen, but have not figured out a clean way to have both the shot and the target go away (removeChild and array.splice) upon collision.
The code I have sort of works, but I keep getting the error,
TypeError: Error #1010: A term is undefined and has no properties. at
DropShootV02_fla::MainTimeline/checkShots()
.
Normally I know that this is because of a mismatch between objects and index numbers, but this is related to the call to a second array in removing boxes and bullets.
For simplicity I'll just include the shooting code. Similar organization creates and drops the boxes.
Any help is appreciated. BTW we are not using external script in an AS file.
var shotSpeed = 18;
var shots:Array = new Array();
import flash.events.MouseEvent;
import flash.events.Event;
stage.addEventListener(MouseEvent.CLICK, fireLaser);
function fireLaser(e:MouseEvent):void
{
if (gameOn==true)
{
var shot:Shot=new Shot();
addChild(shot);
shots.push(shot);
shot.gotoAndStop(1);
shot.x = user.x;
shot.y = user.y;
trace(shots.length);
}
}
addEventListener(Event.ENTER_FRAME, moveShot);
function moveShot(e:Event):void
{
for (var i:int=shots.length-1; i>=0; i--)
{
shots[i].y -= shotSpeed;
if (shots[i].y < -25)
{
removeChild(shots[i]);
shots.splice(i,1);
}
}
}
addEventListener(Event.ENTER_FRAME, checkShots);
function checkShots(e:Event):void
{
for (var i:int=shots.length-1; i>=0; i--)
{
for (var k:int=boxes.length-1; k>=0; k--)
{
if (shots[i].hitTestObject(boxes[k]))
{
if (boxes[i].type == "good")
{
score--;
scoreDisplay.text = "Score:" + score;
}
if (boxes[i].type == "bad")
{
score++;
}
removeChild(boxes[k]);
boxes.splice(k,1);
//if I leave this part out I get no errors,
//but but the bullet goes on to strike again
removeChild(shots[i]);
shots.splice(i,1);
}
}
}
}
Thanks kaarto:
I had tried that previously and kept getting the same error. I used that elsewhere in this game code. Turns out I needed to moderate how often the player was shooting. I changed from shooting with the mouse to using space instead and now the problem is gone. Break is definitely a good one here.
Merge move shot and checkShots in one ENTER_FRAME handler.

Using arrays and hitTest - AS3

From what my friend has told me, this should be working but it is not.
var P2hb:Array = new Array(P2char1, P2char2, P2char3);
var P2life:Number = 0;
addEventListener(Event.ENTER_FRAME, framecheck)
function framecheck(event:Event):void
{
if (P2hb.hitTestObject(P1attack)) { P2life-=2; }
}
This is a generic code but it is the same as what I have. Basically,
all elements in the P2hb are movieclips on the stage.
I want to say that, if P1attack hits any of the objects in the array, then P2life will drop by 2, without having to type hitTestObject() for each individual object.
I can't seem to get it to work. Can anyone tell me what I am doing wrong?
Thank you in advance.
Simply, loop through each individual "movieClip" in the array (using a for loop, for example) and check for the collision against your other object:
function framecheck(event:Event):void
{
for each (var enemy in P2hb) {
if (enemy.hitTestObject(P1attack)) {
P2life-=2;
trace("hit occurred! P2life: "+P2life);
}
}
}

How to print $node inside hook_exit

I have a requirement were I wanted to insert few values depending on whether the node is of type 'news', but when I try to do that with the following code, its seems not working, Could someone help with the code,
function hook_exit() {
if (isset($node) && $node->type == 'event') {
print_r('This is an event');
}
}
According to the Drupal 7 API Reference for hook_exit:
This hook MUST NOT print anything because by the time it runs the response is already sent to the browser.
If you'd like to add information to nodes as they're loaded from the database by Drupal, try using hook_node_load. For example:
function yourmodule_node_load($nodes, $types)
{
foreach ($nodes as $node)
{
// To add or override a node attribute
$node->myvar = "Value";
// To print some data from the node
print_r($node->title);
}
}
To meet my requirement for getting the type, what I did is I have checked the url, took the second argument and passed it as the parameter for node_load function. It is a bit tricky, but it did the job for me.
function tru_statistics_exit() {
if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') {
$nid = arg(1);
$node = node_load($nid);
if ($node->type == 'event') {
get_details_visitor();
}
}
}
Hope some one will find this helpfull

Compare getValue with map.layers[i].name

As part of a xtype combo, I would like to know if the layer I choose in my simple data store (represented by this.getValue()) is present in the map layers. So if it does, A should occur, and B if it does not. The problem is that myLayer variable seems to be unrecognized, even though Opera Dragonify throws no error at all. Where would be the error?
listeners: {
'select': function(combo, record) {
for(var i = 0; i < mapPanel.map.length; i++) {
var myLayer = mapPanel.map.layers[i].name;
if (myLayer == this.getValue()) {
// do A here...
} else {
// do B here...
}
}
}
}
Thanks for any pointers,
I think the problem is that you are using this.getValue() instead of using combo.getValue().
I don't know how your app is set but it's usually a better idea to use the first parameter of your listener instead of the keywork this in order to avoid scope issues.
Hope this helps
#Guilherme Lopes Thanks for that, but the solution was this: mapPanel.map.layers.length instead of mapPanel.map.length.

Resources